C++ Tutorial – 05 – Variables II
Integer types
There are four integer types you can use depending on how large a number you need the variable to hold.
char myChar = 0; // -128 to +127 short myShort = 0; // -32768 to +32767 int myInt = 0; // -2^31 to +2^31-1 long myLong = 0; // -2^31 to +2^31-1
Many C++ compilers also support the long long data type, which is guaranteed to be at least 64-bits. This data type is included in the Microsoft C++ compiler.
long long myL2 = 0; // -2^63 to +2^63-1
To determine the exact size of a data type you can use the sizeof operator. This operator returns the number of bytes that a data type occupies in the system you are compiling for.
std::cout << sizeof(myChar) // 1 byte (per definition) << sizeof(myShort) // 2 << sizeof(myInt) // 4 << sizeof(myLong) // 4 << sizeof(myL2); // 8
The Microsoft C++ compiler features four sized integer types. These types start with __int followed by the number of bits you want the integer to hold – either 8, 16, 32 or 64 bits.
__int8 myInt8 = 0; // 8 bits __int16 myInt16 = 0; // 16 bits __int32 myInt32 = 0; // 32 bits __int64 myInt64 = 0; // 64 bits
Signed and unsigned integers
By default, all the number types in Microsoft C++ are signed and may therefore contain both positive and negative values. To explicitly declare a variable as signed the signed keyword can be used.
signed char myChar = 0; // -128 to +127 signed short myShort = 0; // -32768 to +32767 signed int myInt = 0; // -2^31 to +2^31-1 signed long myLong = 0; // -2^31 to +2^31-1 signed long long myL2= 0; // -2^63 to +2^63-1
If you only need to store positive values you can declare integer types as unsigned to double their upper range.
unsigned char myChar = 0; // 0 to 255 unsigned short myShort = 0; // 0 to 65535 unsigned int myInt = 0; // 0 to 2^32-1 unsigned long myLong = 0; // 0 to 2^32-1 unsigned long long myL2= 0; // 0 to 2^64-1
The signed and unsigned keywords may be used as standalone types, which are short for signed int and unsigned int.
unsigned uInt; // unsigned int signed sInt; // signed int
Similarly, the short and long data types are abbreviations of short int and long int.
short myShort; // short int long myLong; // long int
Floating-point types
The floating-point types can store real numbers with different levels of precision.
float myFloat = 3.14; // 3.4E +/- 38 (7 digits) double myDouble = 3.14; // 1.7E +/- 308 (15 digits) long double myLongDouble = 3.14; // same as double
The precision shown above refers to the total number of digits in the number. For example, trying to assign more than 7 digits to a float means that the least significant digits will get rounded off.
myFloat = 12345.678; // rounded to 12345.68
Floats and doubles can be assigned by using either decimal or exponential notation.
myFloat = 3e2; // 3*10^2 = 300
Char type
The char type is commonly used to represent ASCII characters.
char c = 'x'; // assigns 120 (ASCII for 'x')
The conversion between the number stored in the char and the character shown when the char is printed occurs automatically.
std::cout << c; // prints 'x'
For another integer type to be displayed as a character it has to be explicitly cast to char. An explicit cast is performed by placing the desired data type in parentheses before the variable or constant that is to be converted.
int i = c; // assigns 120 std::cout << i; // prints 120 std::cout << (char)i; // prints 'x'
Bool type
The bool type can store a Boolean value, which is a value that can only be either true or false. These values are specified with the true and false keywords.
bool b = false; // true or false value
![[Affiliate link] C++ Quick Syntax Reference](https://gamingcommission.club/web.archive.org/web/20131121222736im_/d3qzmfcxsyv953.cloudfront.net/images/books/Cpp-quick-syntax-reference-medium.png)
![[Affiliate link] Lynda](https://gamingcommission.club/web.archive.org/web/20131121222736im_/d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

![[Affiliate link] bookname](https://gamingcommission.club/web.archive.org/web/20131121222736im_/d3qzmfcxsyv953.cloudfront.net/images/books/Cpp-quick-syntax-reference-small.png)