Now that we are able to accept input and print output externally, we need a mechanism to store data inside our program.
A variable is simply an named container that stores some value.
int x = 5;
In the example above,
int indicates the type of the variable
int
x is the name of the variable
x
5 is the value of the variable
5
int age = 24
Integer (whole numbers)
double
double pi = 3.14
Floating point numbers
char
char x = 'c'
Singular character
bool
bool isGood = true
True / False
std::string
std::string name = "Benn"
Sequence of characters
⚠️ Common Pitfall
Use double quotes (" ") for strings and single quotes ' ' for characters
" "
' '
Declaration is when we define a variable's type and name without assigning it a value yet. Initialisation is when we give it a value.
Last updated 29 minutes ago
Was this helpful?
int x; // declaration x = 10; // assignment int y = 10; // declaration + initialisation