Skip to main content

C++ Variable

Variable is an identifier, this are play important role in c++ programming language. When we are declare variables inside our source code and compile it. Then compiler are allocated memory of those variable. This memory are exist in RAM (Random-access memory) area. And help of variable name we are access this memory such as assign value and get the value. and memory are contain various bits (1 byte 8 bit). and every byte has memory address.so most import point every variable are has address this are link to actual memory location internally. when we are use of variable. then compiler are deal with particular memory on CPU.

Syntax of variable declaration.

data_type variable_name;
Example
int age; //age is integer variable
float marks; //marks is float variable
double salary; //salary is double variable
char status; //status is character variable

Note that the name of variable is user defined. if we are replace int age to float age. then it age are capable to store float type value. so name is not constant we can used any name but this name cannot be reserved word (identifier).

How to assign value of variable

We can declare any variable of our program. If there are not assign any value to those variable.then compiler will assign default value. If variable are local then compiler will assign garbage value. And if we utilize un-initialized variable in any operation then compiler can produced wrong result. and possible to crash our entire program. best way first assign the variable value and use it variable.

There are two way to assign value of variables

int info1=10; //copy value initialization
int info2(20);// direct initialization

Note that both info1 and info2 are integer variable. in first info1 is assigned the copy of value 10 so this is called copy initialization. and second one is direct initialization. see the example

/*
  Example variable initialization
*/
#include<iostream>
using namespace std;

int main(){
  //Defined variables 
  int num1=10; //copy initialization  
  int num2(20); //direct initialization

  //display variable value
  cout<<"num1 :"<<num1<<endl;
  cout<<"num2 :"<<num2;
}
Output
num1 :10
num2 :20

Note that 'int num2(20);' is not an function. this is direct initialization. but note that here defined and initialization variable in a single statement. therefore compiler are understand, defined is variable not an function. we can use direct initialization when defined the variable. after this statement we are not initialized this variable directly. clear this point using this example.

/*
  Example of direct initialization
*/
#include<iostream>
using namespace std;

int main(){
  
  int num2(20); //direct initialization
  cout<<"num2 :"<<num2;
  num2(45); //error of this line
}
Error like this
error: 'num2' cannot be used as a function

so this reason most of programer are avoid direct initialization of variables. copy initialize is normally used.

About to variables

1) Name of variable is an identifier.

2) Every variable is unique address. only register variable is no address.

3) No need of any header file to declare and define the variable. see the example

int main(){
  //declare and defined variables
  char status='1';
  float pi=3.1415f;
  double costs=194563.33;
  short int age;
}

4) Scope of variable are normally three types. local global and file level.

5) We are can not declare two same name of variable in same scope.

6) variable name is not a keyword.

7) help of (&) operator, we are get and print the address of variables. see example.

/*
* print address of variable
*/
#include <iostream>
using namespace std;
int main(){
  //auxiliary are integer variable
  int auxiliary=100; 
  //printed address of variable using of & operator
  cout<<"Address of auxiliary: "<<&auxiliary <<endl;
  //print value of auxiliary variable
  cout<<"Value of auxiliary  :"<<auxiliary;
}
Output
Address of auxiliary: 0x61ff0c
Value of auxiliary  :100

In this program we are print simply address of integer variable. and similarly we can print the address of another variables. Note that when we are run this program of other system. than there are possible to print another result of variable address.

8) pointer is special type of variables. which are store the address of another variable.

9) There are possible to we are declare two variable are same address. see this examples.

/*
* Two variable are same address
*/
#include <iostream>
using namespace std;
int main(){
  //auxiliary are integer variable
  int auxiliary=100; 
  //defind the same address of check variable
  int &check=auxiliary;
  //printed address of variable using of & operator
  cout<<"Address of auxiliary: "<<&auxiliary <<endl;
  cout<<"Address of check    : "<<&check <<endl;
  cout<<" Value of auxiliary :"<<auxiliary <<endl;
  cout<<" Value of check     :"<<check;
}
Output
Address of auxiliary: 0x61ff08
Address of check    : 0x61ff08
 Value of auxiliary :100
 Value of check     :100

Note that Normally every variable are unique address. but special cases, we can declare same address of two different variables. Now variable are use same memory location. if we are change the value of one variable they are automatically reflect this value to another one. look at view this example.

/*
* Two variable are same address
*/
#include <iostream>
using namespace std;
int main(){
  //auxiliary are integer variable
  int auxiliary=100; 
  //defined the same address of check variable
  int &check=auxiliary;
  check=200;//change value
  //printed address of variable using of & operator
  cout<<"Address of auxiliary: "<<&auxiliary <<endl;
  cout<<"Address of check    : "<<&check <<endl;
  cout<<" Value of auxiliary :"<<auxiliary <<endl;
  cout<<" Value of check     :"<<check;
}
Output
Address of auxiliary: 0x61ff08
Address of check    : 0x61ff08
 Value of auxiliary :200
 Value of check     :200

In this program auxiliary integer variable address are allocated by compiler. and this address are allocated to check integer variable





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment