Literals/constants are used to represent fixed values which can't be altered later in the code.
Interger literals are numeric literals. Below are the examples of various types of literals
79 // decimal (base 10)
0253 // octal (base 8)
0x4F // hexadecimal (base 16)
22 // int
53u // unsigned int
79l // long
7953ul // unsigned longFloat point literals are also numeric literals but has either a fractional form or an exponent form.
79.22 // valid
79E-5L // valid
53E // not valid as it is incomplete exponent
.e22 // not valid as missing integer or fractionThere are two Boolean literals which are part of standard C++ keywords −
-
true value representing true.
-
false value representing false
Character literals are represented with in single quotes. For example, a, 1 etc. A character literal can be a simple character (e.g., 'a'), an escape sequence (e.g., '\n'), or a universal character (e.g., '\u02C0').
String literals are represented with in double quotes. String literals contains series of characters which can be plain characters, escape sequence or a universal character.
"Hello World"You can use either #define or const to define constants as shown below.
- Using #define
#define identifier-name value- Using Const
const datatype variable-name = value;