๐ 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
| Feature | const | #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 constorconstexpr
- Literals are fixed values like 42,'A',3.14, or"text"
- Prefer constover#definefor 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 :
