A variable is a reserved space in memory with a symbolic name (identifier), in which we can store a value of a specific type.
Variables allow us to store temporary data that we can use later throughout our program.
If you want to learn more about Variables
consult the Introduction to Programming Course read more
Declaration of variables
Declaring a variable in C++ involves specifying its type and giving it a name. The basic syntax for declaring a variable is:
type name;
- type: Specifies the data type of the variable.
- name: Is the unique name given to the variable.
For example, to declare an integer variable called age
:
int age;
Assignment of values
Assigning a value to a variable is giving it an initial value or modifying its existing value. The syntax for assigning a value to a variable is:
variableName = value;
For example, to assign the value 25
to the variable age
:
age = 25;
It is also possible to declare and assign a variable in a single line:
int age = 25;
Default values
C++ does not automatically initialize variables with default values. Uninitialized local variables contain garbage binary (a random value) until they are explicitly assigned a value.
For value types (int, float, double, char, bool, etc.), it is recommended to initialize them at the time of declaration:
int age = 0;
float height = 0.0f;
double weight = 0.0;
char initial = '\0';
bool isStudent = false;
For reference types (like pointers), the default value is nullptr
, which indicates that the pointer does not reference any memory location.
int* pointer = nullptr;
std::string* string = nullptr;
This is a frequent cause of error when using an uninitialized variable, especially when we start programming in C++ from other languages.
Volatile variables
volatile
variables indicate to the compiler that the value of the variable may change at any time, outside the control of the program.
volatile int interrupt;
Practical examples
Let’s look at some simple examples of using variables in C++. These examples show how to declare and use variables to perform calculations and manage data.
Managing student data
In this example, we show how to manage basic data of a student, such as their name and age. This example illustrates how to declare and assign values to variables of different data types.
#include <iostream>
#include <string>
int main() {
// We declare variables
std::string name; // for the student's name
int age; // for the student's age
// We assign values to the variables
name = "Ana";
age = 21;
// We print the student's data to the console
std::cout << name << " is " << age << " years old" << std::endl;
return 0;
}
Area calculation of a circle
In this example, we calculate the area of a circle using a basic mathematical formula. A constant is declared for the value of PI and a variable for the radius of the circle.
#include <iostream>
int main() {
// We declare a constant for the value of PI
const double PI = 3.14159;
// We declare a variable for the radius of the circle
double radius = 5.0;
// We calculate the area of the circle using the formula: area = PI * radius^2
double area = PI * radius * radius;
// We print the result to the console
std::cout << "The area of the circle is: " << area << std::endl;
return 0;
}