Constants
A constant integer number will be regarded by default as the
int type,
unless it is too large for such type (in that case it will be regarded as
a
long int). A floating point number will be regarded by default as
double instead (
long double if it is too large).
If necessary, the specific type of a constant can be indicated by using a suffix:
F for
float,
L for
long,
U for
unsigned
(also
f, l, u are valid). Further, a hexadecimal constant can be specified by placing a
0x in front of it, while an octal constant can be specified by placing a
0 in front.
When trying to assign a numerical value of a certain numerical type to a variable of another numerical type, proper conversion is performed,
with possible loss of information (for example when converting from floating-point to integer). Also, any numerical value different from
0 converts to true when assigned to a
bool, while a
bool, when assigned to a numeric value, converts to
0 if its value is
false; it converts to
1 if the value is
true.
Example
#include <iostream>
using namespace std;
int main()
{
int hexadecimal = 0xA;
cout << hexadecimal << "\n\n";
int octal = 011;
cout << octal << "\n\n";
int j = 3.5; // converts double into int - INFO IS LOST!
cout << j << "\n\n";
j = true; // converts bool to int
cout << j << "\n\n";
j = false;
cout << j << "\n\n";
bool b = 2; // converts int to bool - INFO IS LOST: CONVERTS TO 1
cout << b << "\n\n";
b = 5.1; // converts double to bool - INFO IS LOST: CONVERTS TO 1
cout << b << "\n\n";
float f = 3.12345; // converts double into float - POTENTIAL INFO LOSS
cout << f << "\n\n";
double d1 = 3.5f; // converts float into double
cout << d1 << "\n\n";
int m = 12345L; // converts long int into int - POTENTIAL INFO LOSS
cout << m << "\n\n";
int n = 234U; // converts unsigned into signed - POTENTIAL INFO LOSS
cout << n << "\n\n";
double d2 = 132.234L; // converts long double into double - POTENTIAL INFO LOSS
cout << d2 << "\n\n";
};
Output
10
9
3
1
0
1
1
3.12345
3.5
12345
234
132.234