03 - Introduction to C++ Variables

01 Variables Introduction 

In computing, you need to store and retrieve data. Data is stored in the memory of the computer. To retrieve data, you need to know where the data is stored in the memory. Variables help you access data stored in the memory. You can think of them as placeholders.

Let’s look at an example to understand the concept of variables:

In arithmetic, let’s consider you are given the following equation:           

       z=x+y; where x=10 and y=5

In the expression z=x+y, you substitute 10 in place of x and 5 in place of y.

       z= 10+5, which is 15

Thus, x, y, and z are place holders. 

In programming, these place holders are referred to as variables.

In C++ programming, a variable is a name given to a memory location. The value assigned toa variable is stored in the memory allocated to that variable.

A diagrammatic representation of the concept of variables stored in memory is as follows:

             

Another example, in programming, is of an employee database. An employee database contains data such as, Employee Name, Age, Salary, and Date of Joining and so on. In C++, to store this data, you would declare the following as variables:

  • EmployeeName
  • Age
  • Salary
  • DateofJoining
  •  

1.1  Declaration Statement

A declaration statement specifies the variable name and its data type. In C++, you must declare a variable before you can use it in your program. Based on the data type of the variable, memory is allocated.

Let’s say you want to add two whole numbers and display the result onthe screen. In this case, you need to store three values – the two numbers that you want to add and their sum. Thus, you need three variables; sum= x+y

In C++, you declare the variables as follows:

int x;   // declaration statement

               int y;   //declaration statement

              int sum; //declaration statement

1.1.1    Declaring Multiple Variables

You can declare multiple variables in the same line as follows:

              int x, y, sum;

1.2    Assignment Statement

An assignment statement is used to initialize variables. For example,

             x=5;

             y=7;

Let’s say that for an employee database application, you need to set the basic salary to specific value. In C++, you declare a variable that stores the basic salary, and assign it the required value as follows:

            int basicsalary;         //declaration Statement

           basicsalary = 5000;      //assignment statement

Alternatively, you can also write the following:

           int basicsalary = 5000;

In the above statement, we have declared a variable and assigned a value to it the same line.

1.2.1    Unassigned Variables

A variable that has been declared but not assigned a value is known as an unassigned variable. If you use such a variable in an expression, then the compiler throws an error.

Let’s look at an example:

            int basicsalary;

            int bonus;

            int totalsalary

            basicsalary = 5000;

            totalsalary= basicsalary+bonus;

In the above statements, the variable bonus is an unassigned variable, and will throw a compile-time error.

Thus, it’s a good practice to assign values to variables whenever they are declared. 

1.3  Variable Scope

In C++, you can declare variables anywhere in the program. The scope of a variable determines for how long the variable exists in the memory of the computer. If a variable doesn’t exist, you obviously can’t use it in your program.

Based on scope, variables are divided into two categories:

1.3.1    Local Variables

A variable that is declared within a function is known as a local variable. Such a variable exists in the memory as long as the function is being executed. Once the execution of the function is complete, the variable no longer exists in the memory.

If you try to reference a local variable outside the function it was declared in, it generates an error.

Let’s look at C++ program in which you create a function that prints your name.

void displayname()
{
    string name;         // local variable 
    name = Julia;
    cout<<”My name is<<name<<endl;
}
#include <iostream.h>
void main()
{
    displayname();
}

The output of the above program is as follows:

       My name is Julia

In this the variable name is a local variable because it has been declared within the function displayname.

Now, let’s try to use the variable name in the main function:

   void displayname()
{
    string name;         // local variable 
    name = Julia;
    cout<<”My name is<<name<<endl;
}
#include <iostream.h>
void main()
{
    displyname();
    cout<<”Access local variable<<name<<endl;
}

The cout statement will throw an error. This is because, after the execution of the displayname function, the variable name no longer exists in the memory. Hence, the compiler cannot locate the variable name and throws an error.

1.3.2    Global Variables

A variable that is declared in main() is known as global variable. Such a variable exits throughout the execution of a program. In addition, other functions within main() can reference a global variable.Let’s look at C++ program in which you create a function that prints your name. However,in this program,  you will declare the variable name as a global variable.

#include <iostream.h>
void main()
{    string name;
    name = Julia;
        void displayname()
        {
            cout<<”My name is”<<name<<endl;
        }
    cout<<”Accessing global variable”<<name<<endl;
}

In the above program, the variable name is a global variable. This is because the variable has been declared in main and not within the displayname() function. The function displayname() can access the variable name. Also the cout statement does not generate any error, because the variable exists in the memory even after the execution of the displayname() function.

1.4    C++ Program Example

A program in C++, to add two integers (whole numbers) is as follows:

void main()
{
    int x;        //variable declaration
    int y;
    int sum;
    x=5;            //assignment statement
    y=19;
    sum= x+y;        //adding the values stored and storing                 the result 
    cout<<sum<<endl;
}

Understanding the statements in the above program:

          int x;

          int y;

          int sum;

The above statements are declaration statements.

For more information, see Declaration Statement. 

           x=5;

           y=19;

           sum=x+y;

The above statements are assignment statements. The variable sum stores the result of addition. The value stored in sum is 24 (9+15).

           cout<<sum<<endl

The cout statement displays the value stored in the variable sum on the screen.

1.5    Variable sizes and the size of operator

 

Like us on Facebook