-
Constants are expressions with a fixed value.
Literals
-
Literal constants can be classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals.
Postfixes
-
For integers :
-
u/U-
unsigned
-
-
l/L-
long
-
-
ll/LL-
long long
-
75 // int 75u // unsigned int 75l // long 75ul // unsigned long 75lu // unsigned long -
-
For floats :
-
f/F-
float
-
-
l/L-
long double
-
6.02e23f // float 3.14159L // long double -
Prefixes
-
For integers :
-
By default, integer literals are of type
int, but certain suffixes may be appended to an integer literal to specify a different integer type: -
All literals below represent the same
75number. -
0b-
Binary (base-2).
-
-
0-
Octal (base-8).
-
-
0x/0X-
Hexadecimal (base-16).
-
// binary (base-2) 0b01001011 // octal (base-8) 0113 = 1ร8ยฒ + 1ร8ยน + 3ร8โฐ = 64 + 8 + 3 = 75 // decimal (base-10) 75 // hexadecimal (base-16) 0x4b = 4ร16ยน + 11ร16โฐ = 64 + 11 = 75 -
-
For strings
-
u-
char16_t
-
-
U-
char32_t
-
-
L-
wchar_t
-
-
u8-
The string literal is encoded in the executable using UTF-8
-
-
R-
The string literal is a raw string
-
u'a' // char16_t u"hello" // const char16_t[] U'a' // char32_t U"hello" // const char32_t[] L'a' // wchar_t L"hello" // const wchar_t[] u8"hello" // const char[] (C++11), char8_t[] (C++20) R"(C:\path\file.txt)" // Raw string literal -
Floating point Numerals
3.14159 // 3.14159
3.0 // 3.0
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
Other literals
-
true -
false -
nullptr
bool foo = true;
bool bar = false;
int* p = nullptr;
Typed constant expressions
-
Sometimes, it is just convenient to give a name to a constant value:
const double pi = 3.1415926;
const char tab = '\t';
Using Preprocessor Definitions (#define)
-
They have the following form:
#define identifier replacement
-
This replacement is performed by the preprocessor, and happens before the program is compiled.
-
It's sort of blind replacement : the validity of the types or syntax involved is not checked in any way .