Constants

Constants are expressions with fixed value.

Literals

Often are used literals as kind of constants but they are alterable. In previous chapters I have used this kind of literals to initialize a value.


Example:
a = 7;

Literal constants can be classified into integer, floating-point, characters, strings, Boolean, pointers and user defined literals.


Integer Numerals

Integer values are values like -64, 0, 113, 5078 and they are not enclosed in quotes or any other special characters.


All literal constants for integer numbers in C can not alone defined as decimal numbers but it is possible to define it as octal or hexadecimal numbers too.


Example:
1010//decimal
1762//octal
0x3F2//hexadecimal

All of these numbers present the same number 1010. The type by default integer literals are int, but are appended certain suffixes to an integer literal than it's specified to a different integer type.


SuffixType modifier
u or Uunsigned
l or Llong
ll or LLlong long

Unsigned may be combined with any of the other two in any order to form unsigned long or unsigned long long.


Example:
1010//int
1010l//long
1010ul//unsigned long
1010LLU//unsigned long long

The suffix can be specified with upper or lower case letters.


Floating-Point Numerals

Floating point numerals are real values, of integer or decimal values, with or without their exponents. They can include either a decimal point and an e character for example 4e10 that expresses four times ten high ten or by the Xth value of an integer or a decimal value times ten and the Xth height.


Example:
3.14159//PI 3.14159
5.074e25//5.074 x 10^25
2.5e-15//2.5 x 10^-15
1.0//1.0

The default type for floating point literals is double. Floating point type of float or long double can be specified by adding f or F for float and l or L for long double.


Example:
3.14159L//long double
2.5e10f//float

Any of the letters e, f, l can be written in lower or upper case letters with no difference in meaning.


Character and strings literals

Character and strings literals are enclosed in quotes.


Example:
'a'
"Hello world!"

Single characters are enclosed in single quotes (') and strings are enclosed in double quotes (").


Character and string literals can represent special characters handed on C language that forces a newline into output (\n) for example. All this special characters begins with a backslash like \n (for newline) or \t (for tab).


List of escape codes:

Escape CodeDescription
\nnewline
\rcarriage return key
\ttab
\vvertical tab
\bbackspace
\fform feed (page feed)
\aalert (beep)
\'single quote (')
\"double quote (")
\?question mark (?)
\\Backslash (\)

All circuits are understand two states power on and power off or 0 and 1 and internally of computers are represented characters as numerical binary codes. But binary codes are hard to read and extensions of base-8 (octal) or base-16 (hexadecimal) are more readably and this codes are collected into an ASCII table. To using an ASCII code in C by type in a backslash and followed digits for octal (\65) and for hexadecimal followed after backslash an x character and than hexadecimal digits (\x4a).

It is possible to concatenate several separated string literals to a single string literal in C++.

Example:
stringmy_string = "This forms " "a single " "string";

This definition of a string is the same like this.

Example:
stringmy_string = "This forms a single string";

A string can wrote into seperate lines with this trick:

my_string = "This is a string in \
two lines";

All the character literals and string literals descriped above are made of characters of type char. A different character type can be specified by using one of the following prefixes:

PrefixCharacter Type
uchar16_t
Uchar32_t
Lwchar_t

All prefixes are case sensitive for example lowercase for char16_t and uppercase for char32_t and wchar_t.


For string literals exists prefixes as follows:

PrefixDescription
u8The string literal is encoded in the executable using UTF-8
RThe string literal is a raw string

In raw strings, backslashes and single and double quotes are all valid characters. The content of the literal is delimited by an initial R"sequence( and a final )sequence", where sequence is any sequence of characters (including an empty sequence). The content of the string is what lies inside the parenthesis, ignoring the delimiting sequence itself.

Example:
R"(string with \\backslash)
R"&%$(string with \\backslash)&%$"

Both strings are equivalent to "string with \\backslash". The R prefix can be combined with other prefixes, such as u, L or u8 too.


Other literals

Three keyword literals exist in C++: true, false and nullptr:

Example:
boolfoo = true;
boolbar = false;
int*p = nullptr;

Typed constant expressions

It is convinient to define constants for calculations.

Example:
const doublepi = 3.1415926;
const charnewline = '\n';

We can then use these names instead of the literals they were defined to:


1#include <iostream>
2using namespace std;
3
4const double pi = 3.1415926;
5const char newline = '\n';
6double circumference(double); //function prototype
7
8int main(int argc, char **argv) {
9    double radius;
10    
11    cout << "Please insert radius: ";
12    cin >> radius;
13    
14    cout << "The circumference is: " << circumference(radius) << newline;
15    
16    return 0;
17}
18
19double circumference(double radius) {
20    return 2 * pi * radius;
21}

Preprocessor definitions (#define)

With preprocessor definitions are possible to define name constants too. They have the following form:

#define identifier replacement

Such one defined directive is performed by preprocessor befor the program is compiled and is not checked of validity or syntax. It can be a sequence of characters or a constant like PI.

Example:

1#include <iostream>
2using namespace std;
3
4#define PI 3.14159
5#define NEWLINE '\n'
6
7int main(int argc, char **argv) {
8    double r = 5.0; // radius
9    double circle;
10
11    circle = 2 * PI * r;
12    cout << circle;
13    cout << NEWLINE;
14
15    return 0;
16}

Note that the #define lines are preprocessor directives, and as such are single-line instructions that -unlike C++ statements- do not require semicolons (;) at the end; the directive extends automatically until the end of the line. If a semicolon is included in the line, it is part of the replacement sequence and is also included in all replaced occurrences.