Working with C++ Variables

Variables differ slightly when using C++ as opposed to Python. In Python, you can simply state that ‘a’ equals 10 and a variable is assigned. However, in C++ a variable has to be declared with its type before it can be used.

You can declare a C++ variable by using statements within the code. There are several distinct types of variables you can declare. Here’s how it works.

Step 1 – Open up a new, blank C++ file and enter the usual code headers:

#include <iostream>
using namespace std;
int main()



Step 2 – Start simple by creating two variables, a and b, with one having a value of 10 and the other 5. You can use the data type int to declare these variables. Within the curly brackets, enter:

int a;
int b;

a = 10;
b = 5;

Step 3 – You can build and run the code but it won’t do much, other than store the values 10 and 5 to the integers a and b. To output the contents of the variables, add:

cout << a;
cout << ”\n”;
cout << b;

The cout << “\n”; part simply places a new line between the output of 10 and 5.

Step 4 – Naturally you can declare a new variable, call it result and output some simple arithmetic:

int result;

result = a + b;
cout << result;

Insert the above into the code as per the screenshot.

Step 5 – You can assign a value to a variable as soon as you declare it. The code you’ve typed in could look like this, instead:

int a = 10;
int b = 5;
int result = a + b;

cout << result;

Step 6 – Specific to C++, you can also use the following to assign values to a variable as soon as you declare them:

int a (10);
int b (5);

Then, from the C++ 2011 standard, using curly brackets:

int result {a+b};

Step 7 – What you’ve used so far are local variables: variables used inside the function. You can also create global variables, which are variables that are declared outside any function and used in any function within the entire code. For example:

#include <iostream>
using namespace std;
int StartLives = 3;

int main ()

startLives = StartLives – 1;
cout << StartLives;

Step 8 – The previous step creates the variable StartLives, which is a global variable. In a game, for example, a player’s lives go up or down depending on how well or how bad they’re doing. When the player restarts the game, the StartLives returns to its default state: 3. Here we’ve assigned 3 lives, then subtracted 1, leaving 2 lives left.

Step 9 – The modern C++ compiler is far more intelligent than many programmers assume. While there are numerous data types you can declare for variables, you can in fact use the auto feature:

#include <iostream>
using namespace std;
auto pi = 3.141593;

int main()
double area, radius = 1.5;
area = pi * radius * radius;
cout << area;

A couple of new elements here: first, auto won’t work unless you go to Settings > Compiler and tick the box labelled ‘Have G++ follow the C++11 ISO C++ Language Standard [-std=c++1]’. Then, the new data type, double, which means double-precision floating point value. Enable C++11, then build and run the code. The result should be 7.06858.

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button