c++ variables

Variables are named portions of memory that you define to store data. Every variable can store a particular type of data called data type of the variable. The data stored in variables can be changed while executing our program. The size of the variable depends upon the type of data the variable has to store. We can perform all arithmetic operations on data stored in variables through arithmetic operators. Before use, every c++ variable should be declared. The variable should be initialized with some initial value otherwise it will store junk values.

How we can declare a c++ variable?

Declaration of a variable has two parts
  1. Type of data the variable should store
  2. Name of the variable
1)The data type of the variable is the type of data that the variable is declared to store. C++ has many built-in data types with different sizes and storing capabilities. For example, int data type can store integer values, char data type is capable of storing characters. You can learn more in our post where we have explained each and everything about data types. Some fundamental data types are shown in the figure below.

2) There are some rules which must be followed while writing a variable name. 
  1. You can use lower and upper case letters 
  2. you can include underscores
  3. Digits can be used but a variable name can not start with a digit.
  4. We can not use special characters like !, @, $, #, etc.
  5. Empty spaces are also not allowed 

How to name a variable?

  1. As we know that c++ is a case sensitive language so two variables with the exact same name and a difference of a capital letter will be considered as two different variables. For example int Quantity and int quantity are two different variables. 
  2. A variable name should not match any keyword. The words having predefined meaning in c++ library are called keywords. For example, class, bool, const , for, etc.
  3. The variable should indicate its purpose. Besides, the name of the variable should express what sort of data the variable will store. For example, it's better to use float marks rather than float m to store marks of students.
  4. The variable name should be kept as short as possible.

What is the lifetime of a variable?

The existence of all variables is finite. The lifetime of any variable is the existence of a variable from the point of declaration to its destruction. There are three types of storage duration described below.
  1. Static variables are declared with the static keyword and have static storage duration. The lifetime of such variables starts from their declaration to the end of the execution of the program. If not initialized manually, such variables are initialized to zero by default.
  2. The variable declared within a block survives from the point they are declared to the ending brace which is the end of the block of code. This is called automatic storage duration. This means such variables are destroyed automatically when the block ends(ending brace).
  3.  In dynamic storage duration, we allocate memory for a variable on runtime. The existence of such variables are from the point you create it until the memory is released to destroy it. 

What is the scope of a variable

The region of a program where a variable can be accessed is called the scope of a variable. e.g. If a  variable is created with a static keyword within a block of code, its scope is limited to the block but its lifetime is till the end of the program as we have discussed already.

    Different types of variables on the basis of scope


    Local variables

    Local variables are the variables only accessible within the braces or block of code. This means they have local scope. Such variables have automatic storage duration. They are automatically destroyed after the execution of a block of code containing the declaration of a local variable. 

    Global variables

    Global variables are variables available throughout the source file of a program. These variables are declared outside all the functions of a program. These variables have static storage duration. Therefore, they survive from the start until the end of a program. 

    Are two variables with the same name possible?


    1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    #include<iostream>
    using namespace std;
    int number=0;    //global variable
    int main(){
     if(true){
      
      int number=15;    //local variable
      cout<<number<<endl; //printing local variable 
     }
     cout<<number<<endl;  //printing global variable
     return 0;
    }
    

    Yes, it is possible through the concept of local and global variables using their scopes. In the above program, we have two variables with the same name number. The first variable above all the functions has global scope.  The variable within the braces of if statement has local scope and exists only within the braces. So,  our output is 15 and 0. You can confirm it in your own compiler. 

    What are external variables?

    Sometimes, we need to access variables in one source file that are declared in another in programs comprising multiple source files. The extern keyword is helpful for this purpose. Etern keyword means that the variable is declared in other source files and they must not be initialized. Such variables are called external variables.

    Write a program to calculate the wavelength of a wave?


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    #include<iostream>
    using namespace std;
    
    int main(){
     float frequency, wavelength, velocity; //declaration of three variables in one line
     cout<<"enter frequency"<<endl;
     cin>>frequency;     //input frequency
     cout<<"enter velocity"<<endl;
     cin>>velocity;     //input velocity
     wavelength=velocity/frequency; //calculating wavelength using formula
     cout<<"wave length:\t"<<wavelength<<"m"; // displaying wavelength result
     return 0;
    }
    

      Output

      I have covered each and everything related to variables in this post. I hope you found this article helpful. Please don't hesitate to ask questions and give suggestions in the comment section below.

      Post a Comment

      1 Comments