Variables in C++ - TGN

Latest

Welcome to Technicalgurunow! Your No. 1 source for all networking , earning and programming tutorials. We're dedicated to providing you the best of knowledgeable information with a focus on dependability. We hope you enjoy our blog.

Sunday, December 22, 2019

Variables in C++

What are Variables?

  • Variables are used to store any type of values within a program and whose value can be changed during the execution of a program.
  • Variables have different memory requirements and storing capabiltiy based on their data type.

Definition of Variable:-

  • Variable definition means that the programmer provides some instructions to the compiler to create the storage in a memory location.

  • Syntax:-

        data_type variable_name;
        OR
        data_type variable_name, variable_name;

In above syntax, data type can be int, float, char, double and variable list is the lists of variable names to be declared.
Example: 
    int size;
    char letter;
    double d;
    float area;

Initialization of Variable:-

  • Variables can be initialized. That means value can be provided i.e. can be initialized to the variables.

  • Syntax:-

           data_type variable_name=value;

Example:-
int size=10;
char letter=A;
double d=20.2;
float area=10.2f;


Rules of Declaring the Variables in C++ :-

  • A variable name should consist the uppercase letters A-Z, lowercase letters a-z, digits 0-9 and the underscore'_' character.
  • The first character always must be a letter or underscore.
  • Blank spaces cannot be used in a variable name.
  • Special characters like @,#,$,% etc are not allowed.
  • C++ keywords are not allowed as a variable name.
  • Variable names are case sensitive.
  • A variable name can be consisting of 31 characters only if we declare a variable more than one characters, compiler will ignore after 31 characters.
  • Variable type can be bool, char, int, float, double, void.

Example of a Program based on the usage of variables:

#include<iostream>
using namespace std;

int main( )
{
    int i=10;
    int j=20;

   int result;
   result= i+j;
   cout<<result;
}



Types of Variables based on their Scope:-

  • Any variable declared inside the curly braces have scope limited within these curly braces only.
  • If we declare a variable in main( ) function and try to use that variable outside the main( ) function the we will get a compilation error.
  • This is divided into two parts:- 

1) Global Variable:-

    A variable which is declared outside of any function as well as main( )function, then it is known as
    global variable.
    Global variables have their scope throughout the program. That means, they can be accessed 
    anywhere in the program, in the main( ) function, in the user defined function.

Example:

#include<iostream>
using namespace std;

int size= 10;

int main( )
{
    cout<<"size:"<<10;
     return 0;
}


2) Local Variable:-

Local variables are declared inside the braces of any user defined function, main function, loops or any control statements(if, if-else,while, etc).
They have their scope limited inside those braces only.

Example:-

#include<iostream>
using namespace std;

int main( )
{
   int size=10;
   cout<<"size:"<<10;
  
   return 0;
}



Reference Variable:-

  • Reference is considered as an another name for an existing variable.
  • To create a reference '&' operator is used.
  • If '&' operator is written after '='operator, then it is considered as address of operator.
            Example:- 
                 int no=11;
                 int *ptr=&no;

  • If '&'operator is written before '='operator, then it is considered as reference.
            Example:-
                 int no=11;
                 int &i=no;

Incase of reference, no separate memory is allocated for reference.


3 comments:

If you have any query, please let me know

Followers