Variables and Their Applications

Learn about the variables and control structures in C++.

When we start talking about programming, it's always a good start to discuss the variables first.

What is a variable?

In programming, variables differ from their mathematical counterparts. In mathematics, variables represent static values that remain unchanged once assigned. However, in programming, variables act as dynamic placeholders or memory blocks capable of storing and manipulating various data types. Understanding this distinction between the two domains is crucial for comprehending the roles and behaviors of variables in programming.

Note: Within the programming domain, data types and their characteristics are essential knowledge. Different programming languages, including C++, provide a range of primitive types to accommodate different forms of data storage. By understanding the properties and limitations of these data types, programmers can effectively use variables to manipulate and process information accurately. For example, understanding numeric data types like int and float enables the handling of nonfloating point integers and floating point values, respectively. Similarly, the char data type is used for storing and manipulating individual characters within a program.

In C++, a range of primitive types is available to accommodate different forms of data storage. For instance, the int type (capable of storing 32-bit integers) and the long long int type (capable of storing 64-bit integers ) are suitable for storing nonfloating point integer values. On the other hand, the float and double types, with different byte sizes (four and eight bytes, respectively), are utilized for handling floating-point values. The char type serves the purpose of storing characters. Defining a variable in the C++ language follows a specific syntax.

Type variableName = value;

At first, we define the type followed by the variable’s name and then assign the valid state/value to it. The unary operator sizeof is available in the language that shows how much memory has been occupied by a variable of any type.

Let’s take a look at a code example covering several data types.

Press + to interact
#include <iostream>
using namespace std;
int main() {
int a_i = 5;
cout << "a_i = " << a_i << " 'size of a_i is:' " << sizeof(a_i) << '\n';
int ia_li = 9898988998989889; // will generate warning and will not be able to store properly
cout << "ia_li = " << ia_li << "\t\t 'overflow happened' " << '\n';
long long int a_li = 989898989898998;
cout << "a_li = " << a_li << " \t\t 'size of a_li is:' " << sizeof(a_li) << '\n';
float a_f = 5.4;
cout << "a_f = " << a_f << " 'size of a_f is:' " << sizeof(a_f) << '\n';
double a_d = 5.6;
cout << "a_d = " << a_d << " 'size of a_d is:' " << sizeof(a_d) << '\n';
char a_c = 'f';
cout << "a_c = '" << a_c << "' 'size of a_c is:' " << sizeof(a_c) << '\n';
return 0;
}
  • Line 5: We define an int variable a_i of four bytes. It can also store integer values like 5, 0, and -1.

  • Line 7: We define the variable int ia_li to store the value 9898988998989889, which is not possible. So, it will generate a warning of undefined behavior because of the memory overflow.

  • Line 9: We define a long long int variable a_li of eight bytes, which can store the integer value 989898989898998. This is as large as an eight-byte integer can store from 263-2^{63} to +2631.+2^{63}-1.

  • Line 11: We define a float variable a_f of four bytes, which can store real values like 2.3 and 309.112.

  • Line 13: We define a double variable a_d of eight bytes, which can store real values like 3,012.121 and 10,143.90990.

  • Line 15: We define a char variable a_c of one byte, which can store individual characters like 'a', 'b', and '&'.

Understanding code memory segments is imperative to fully comprehending these concepts.

Memory segments

There are four segments of memory. The first one is the global segment (also called the static segment), which can store global variables of the code, making these variables accessible to the whole program. The significant thing about this segment is its size, which remains unchanged during the program. The second one is called the code segment, which stores the code in binary form, and in memory, it is typically read-only and has a fixed size.

The other two segments are heap and stack. When the control of execution enters the block during execution, which is defined by the curly braces ({}), all the variables declared in the block are given a memory on the stack (depending on their type). There is no proper separation between heap and stack; both of them grow in opposite directions. This ensures that the program’s use of memory is efficient. If the program requires more memory from the stack, it grows at both ends.

Let’s look at the code where we’ve created multiple blocks with variables of different types.

Press + to interact
#include <iostream>
using namespace std;
int main()
{
int alpha = 5;
{
char gamma = ‘A’;
}
{
double dVar = 13.019192828278;
{
long long int longVar = 12837127371237981273;
}
bool flag = true;
}
return 0;
}

Let’s look at the following illustration to make it clearer:

The slides clearly describe the memory management for a small program.

Uses of variables

Let’s look at a few other examples that depict how we can use the variables to solve several arithmetic problems.

Arithmetic operations

We can perform five different arithmetic operations on two operands of primitive types: addition, subtraction, division, modulus (it can not work for floating points), and multiplication. The given table describes the behavior of these operations when applied to different values.

Binary Operators

Symbol

Operation

Examples (On Integer Operands)

+

Addition

13 + 5 replaced by 18

-

Subtraction

13 - 5 replaced by 8

*

Multiplication

13 * 5 replaced by 65

/

Division

13 / 5 replaced by 2

%

Modulus

13 % 5 replaced by 3

But what if the operands on which we’re going to perform the arithmetic operations are of different types? For example, what if one is of the int type and the other is of the float type? What should be the type of the result variable in which we’ll store the result?

Let’s take a look at the following example and see how it works:

Let’s consider an example that tests the arithmetic manipulation of multiple variables.

Press + to interact
#include <iostream>
using namespace std;
int main()
{
cout << "<<Integer type>>" << '\n';
int a = 5;
int b = 4;
int c = a + b;
cout << "The value of a = " << a << '\n';
cout << "The value of b = " << b << '\n';
cout << "The sum of a and b is: " << c << '\n';
cout << "\n<<Float type>>" << '\n';
float aF = 5.1;
float bF = 4.0;
float cF = aF - bF;
cout << "The value of aF = " << aF << '\n';
cout << "The value of bF = " << bF << '\n';
// use of arithimatic operations
cout << "The difference of aF and bF is: " << cF << '\n';
cout << "\n<<Double type>>" << '\n';
double aD = 5.0;
double bD = 4.2;
double cD = aD + bD;
cout << "The value of aD = " << aD << '\n';
cout << "The value of bD = " << bD << '\n';
// use of arithimatic operations
cout << "The sum of aD and bD is: " << cD << '\n';
cout << "\n<<Long Long type>>" << '\n';
long long int aLl = 5;
long long int bLl = 4;
long long cLl = aLl + bLl;
cout << "The value of aLl = " << aLl << '\n';
cout << "The value of bLl = " << bLl << '\n';
// use of arithimatic operations
cout << "The sum of aD and bD is: " << cLl << '\n';
cout << "\n<<Char type>>" << '\n';
char aC = 'a';
cout <<"The value of char aC is " << aC << '\n';
return 0;
}
  • Lines 6–9: We define two variables, a and b, with values 5 and 4 and then store the sum of those two in the c variable.

  • Lines 10–12: We display the values of the a, b, and c variables.

  • Lines 14–16: We store the subtraction of two float type variables, aF and bF, in the third float type variable, cF.

  • Lines 17–20: We display the values of the aF, bF, and cF variables.

  • Lines 22–24: We define two double type variables, aD and bD, and store their product in the third double type variable, cD.

  • Lines 25–28: We display the values of the aD, bD, and cD variables.

  • Lines 30–32: We define two variables, aLl and bLl, of long long int type and then store their sum in the third variable, cLl, of the same type.

  • Line 38: We define a char variable aC with the 'a' value.

We can play with the code by changing the operators and observing the change in the output.