Variables and Their Applications
Learn about the variables and control structures in C++.
We'll cover the following
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
andfloat
enables the handling of nonfloating point integers and floating point values, respectively. Similarly, thechar
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.
#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 properlycout << "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
variablea_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 value9898988998989889
, 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
variablea_li
of eight bytes, which can store the integer value989898989898998
. This is as large as an eight-byte integer can store fromto Line 11: We define a
float
variablea_f
of four bytes, which can store real values like 2.3 and 309.112.Line 13: We define a
double
variablea_d
of eight bytes, which can store real values like 3,012.121 and 10,143.90990.Line 15: We define a
char
variablea_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.
#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.
#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 operationscout << "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 operationscout << "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 operationscout << "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
andb
, with values5
and4
and then store the sum of those two in thec
variable.Lines 10–12: We display the values of the
a
,b
, andc
variables.Lines 14–16: We store the subtraction of two
float
type variables,aF
andbF
, in the thirdfloat
type variable,cF
.Lines 17–20: We display the values of the
aF
,bF
, andcF
variables.Lines 22–24: We define two
double
type variables,aD
andbD
, and store their product in the thirddouble
type variable,cD
.Lines 25–28: We display the values of the
aD
,bD
, andcD
variables.Lines 30–32: We define two variables,
aLl
andbLl
, oflong long int
type and then store their sum in the third variable,cLl
, of the same type.Line 38: We define a
char
variableaC
with the'a'
value.
We can play with the code by changing the operators and observing the change in the output.