Variables & Types

In C++ we need to give each variable a type (unlike Python which knows what type a piece of data is).

How to declare the variable a as an integer with the value 2:

int a = 2;

Most common data types

short or short int - short integer - 2 bytes

int - integer - 4 bytes

long or long int - long integer - 4 bytes

bool - Boolean (T/F) - 1 byte

float - floating point number - 4 bytes

double - double precision floating point number - 8 bytes

char - character - 1 byte

Global versus local

Before we use a variable we need to declare it somewhere. Depending on where we declare it the scope will be global or local.

Global variables

Global variables are declared in the main code (not in a function) and can be used anywhere.

Local variables

Local variables are declared inside a function or block enclosed in {} and can only be used within the block they were declared in.