C++ Constants & Literals (Full Explanation)
In programming, there are many situations where we need fixed values that should never change while the program runs. These fixed values are called constants in C++. They are also known as literals because they directly represent their value in the code.
Just like variables, constants are used to store data. The only difference is that a variable’s value can be updated, but a constant’s value remains fixed once it is defined. If you try to change a constant later, the compiler will show an error.
In C++, constants can be created using different data types. They are commonly divided into:
- Integer literals
- Floating-point literals
- Boolean literals
- Character literals
- String literals
Integer Literals
An integer literal represents a whole number. In C++, integers can be written in three number systems:
- Decimal (base 10): Normal numbers without any prefix. Example →
85
- Octal (base 8): Starts with
0
. Example →0213
- Hexadecimal (base 16): Starts with
0x
or0X
. Example →0x4b
C++ also allows suffixes like U
(unsigned) and L
(long). The suffix can be uppercase or lowercase and written in any order.
Valid examples:
212 // Legal
215u // Legal
0xFeeL // Legal
Invalid examples:
078 // Illegal: 8 is not an octal digit
032UU // Illegal: cannot repeat a suffix
Other examples of integer literals:
85 // decimal
0213 // octal
0x4b // hexadecimal
30 // int
30u // unsigned int
30l // long
30ul // unsigned long
Floating-Point Literals
Floating-point literals are used to represent numbers with fractions or decimals. They can be written in two forms:
-
Decimal form – contains a decimal point, an exponent, or both.
-
Exponential form – uses
e
orE
to represent the power of 10.
Valid examples:
3.14159 // Legal
314159E-5L // Legal
Invalid examples:
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent
.e55 // Illegal: missing integer or fraction
Boolean Literals
C++ provides two boolean literals:
true
→ represents logical truthfalse
→ represents logical false
These values should not be confused with integers 1
and 0
. Even though they may act similarly in some cases, they are separate boolean types in C++.
Character Literals
Character literals are used to store single characters. They are written inside single quotes.
- A plain character →
'x'
- A wide character →
L'x'
(stored inwchar_t
) - An escape sequence →
'\t'
(tab) - A universal character →
'\u02C0'
Some characters in C++ have special meanings when written with a backslash (\
). These are called escape sequences.
Escape sequence | Meaning |
---|---|
\\ |
Backslash |
\' |
Single quote |
\" |
Double quote |
\n |
Newline |
\t |
Tab |
\a |
Alert/Bell |
\b |
Backspace |
\r |
Carriage return |
\f |
Form feed |
\v |
Vertical tab |
\ooo |
Octal value |
\xhh |
Hexadecimal value |
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello\tWorld\n\n";
return 0;
}
Output:
"hello, World"
String Literals
A string literal is a sequence of characters enclosed in double quotes. Strings can include normal characters, escape sequences, and universal characters.
C++ also allows long strings to be split across multiple lines. The compiler will join them automatically.
Examples:
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
All of the above represent the same string.
Defining Constants in C++
There are two common ways to define constants in C++.
1. Using #define
preprocessor directive
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
Output:
50
2. Using const
keyword
#include <iostream>
using namespace std;
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}
Output:
50
Note
By convention, constants in C++ are usually written in UPPERCASE letters. This makes them easy to recognize in the code.