โœ๏ธ C++ Basic Syntax & Language Elements
Estimated reading: 3 minutes 37 views

๐Ÿ”’ C++ Constants and Literals โ€“ Fixed Values in Your Code


๐Ÿงฒ Introduction โ€“ What Are Constants and Literals in C++?

In C++, constants and literals represent fixed values that do not change during program execution. Constants are often used to make code more readable, maintainable, and secure by preventing accidental changes to important values.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What constants and literals are
  • Types of literals in C++
  • How to define and use constants
  • Key differences between const, #define, and literals

๐Ÿ” What Are Constants in C++?

A constant is a named value that cannot be modified after its declaration.

โœ… Declaring Constants Using const Keyword

const float PI = 3.14159;
const int MAX_USERS = 100;
  • Cannot be changed after initialization
  • Must be initialized during declaration

๐Ÿงฎ What Are Literals?

A literal is a fixed value used directly in the code. It can represent:

  • Numbers (42)
  • Characters ('A')
  • Strings ("Hello")
  • Boolean values (true, false)

๐Ÿ”ข Types of Literals in C++

1๏ธโƒฃ Integer Literals

int num = 100;
int hex = 0xFF;     // Hexadecimal
int bin = 0b1010;   // Binary (C++14+)

2๏ธโƒฃ Floating-point Literals

float pi = 3.14f;
double gravity = 9.8;

3๏ธโƒฃ Character Literals

char grade = 'A';
char newline = '\n';

4๏ธโƒฃ String Literals

string message = "Welcome!";

โš ๏ธ In C++, string literals like "Hello" are of type const char[].


5๏ธโƒฃ Boolean Literals

bool isOnline = true;
bool isDead = false;

6๏ธโƒฃ Null Pointer Literal (C++11)

int* ptr = nullptr;   // Preferred over NULL

โœ๏ธ Example โ€“ Constants & Literals Together

#include <iostream>
using namespace std;

int main() {
    const float TAX_RATE = 0.18;   // Constant
    float price = 100.0;           // Literal 100.0
    float tax = price * TAX_RATE;

    cout << "Tax: " << tax << endl;
    return 0;
}

๐Ÿ†š const vs #define for Constants

Featureconst#define
Type-safeโœ… YesโŒ No
Scopedโœ… Respects C++ scoping rulesโŒ Global by default
Debuggableโœ… Better error messagesโŒ No type info during debugging
Preferred in C++โœ… Use const, constexpr, enumโŒ Legacy C macro usage

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

๐Ÿ” Key Takeaways:

  • Constants are fixed variables defined using const or constexpr
  • Literals are fixed values like 42, 'A', 3.14, or "text"
  • Prefer const over #define for type safety and maintainability

โš™๏ธ Real-World Relevance:
Using constants and literals correctly reduces hard-coded values, improves code clarity, and prevents bugs from accidental value changes.


โ“ FAQs โ€“ C++ Constants and Literals

โ“ What is the difference between a constant and a literal?
โœ… A constant is a named value (const int max = 10;), while a literal is a raw value (10).

โ“ Can I change a const value later?
โŒ No. Once declared, a const variable cannot be modified.

โ“ Should I use #define or const in C++?
โœ… Use const or constexprโ€”they are type-safe and scoped.

โ“ What is a string literal?
โœ… A string value enclosed in double quotes, e.g., "Hello".

โ“ What is nullptr?
โœ… It is a C++11 keyword representing a null pointer value, preferred over NULL.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

C++ Constants and Literals

Or Copy Link

CONTENTS
Scroll to Top