C++ Tutorial – 04 – Variables
Variables are used for storing data during program execution.
Data types
Depending on what data you need to store there are several kinds of built-in data types. These are often called fundamental data types or primitives. The integer (whole number) types are short, int and long. The float, double and long double types are floating-point (real number) types. The char type holds a single character and the bool type contains either a true or false value.
| Data Type | Size (byte) | Description |
|---|---|---|
| char | 1 | Integer or character |
| short int long | 2 4 4 | Integer |
| float double long double | 4 8 8 | Floating-point number |
| bool | 1 | Boolean value |
In C++, the exact size and range of the data types are not fixed. Instead they are dependent on the system for which the program is compiled. The sizes shown in the table above are those found on most 32-bit systems and are given in C++ bytes. A byte in C++ is the minimum addressable unit of memory, which is guaranteed to be at least 8 bits, but might also be 16 or 32 bits depending on the system. By definition, a char in C++ is 1 byte in size. Furthermore, the int type will have the same size as the processor’s word size, so for a 32-bit system the integers will be 32 bits in size. Each integer type in the table must also be at least as large as the one preceding it. The same applies to floating-point types where each one must provide at least as much precision as the preceding one.
Declaring variables
To declare (create) a variable you start with the data type you want the variable to hold followed by an identifier, which is the name of the variable. The name can consist of letters, numbers and underscores, but it cannot start with a number. It also cannot contain spaces or special characters and must not be a reserved keyword.
int myInt; // correct int _myInt32; // correct int 32Int; // incorrect (starts with number) int Int 32; // incorrect (contains space) int Int@32; // incorrect (contains special character) int new; // incorrect (reserved keyword)
Assigning variables
To assign a value to a declared variable the equals sign is used, which is called the assignment operator (=).
myInt = 50;
The declaration and assignment can be combined into a single statement. When a variable is assigned a value it then becomes defined.
int myInt = 50;
At the same time that the variable is declared there is an alternative way of assigning, or initializing, it by enclosing the value in parentheses. This is known as constructor initialization and is equivalent to the statement above.
int myAlt (50);
If you need to create more than one variable of the same type there is a shorthand way of doing it using the comma operator (,).
int x = 1, y = 2, z;
Octal and hexadecimal assignment
In addition to standard decimal notation, integers can also be assigned by using octal or hexadecimal notation. Both numbers below represent the same number, which in decimal notation is 50.
int myOct = 062; // octal notation (0) int myHex = 0x32; // hexadecimal notation (0x)
Using variables
Once a variable has been defined (declared and assigned) you can use it by simply referencing the variable’s name, for example to print it.
std::cout << myInt << myAlt; // "5050"
Variable scope
Variables in C++ may be declared both globally and locally. A global variable is declared outside of any code blocks and will be accessible from anywhere after it has been declared, even in other source files in the same project. A local variable, on the other hand, is declared inside of a function and will only be accessible within that function after it has been declared. The lifetime of a local variable is also limited. A global variable will remain allocated for the duration of the program, while a local variable will be destroyed when its function has finished executing.
int globalVar; // global variable int main() { int localVar; } // local variable
Default values
Global variables in C++ are automatically initialized to zero. Local variables however do not have this advantage. Instead, they will contain whatever garbage is left in that memory location. It is therefore a good idea to always give your local variables an initial value when they are declared.
int globalVar; // initialized to 0 int main() { int localVar; } // uninitialized
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/20140118011025im_/d3qzmfcxsyv953.cloudfront.net/images/books/Cpp-quick-syntax-reference-medium.png)
![[Affiliate link] Lynda](https://gamingcommission.club/web.archive.org/web/20140118011025im_/d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

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