News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

C++ Tutorial – 04 – Variables I

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. Float, double and long double are floating-point (real number) types. Char holds a single character and the bool type contains either a true or false value.

Data TypeSize (byte)Description
char1Integer or character
short
int
long
2
4
4
Integer
float
double
long double
4
8
8
Floating-point numbers
bool1Boolean 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

If you like this tutorial please +1 it:


Leave a Reply