Friday 6 March 2015

Assignment Operator in C++

Assignment Operator in C++ (=)   Assignment operator is use to assign value to variable or assign the value of one variable to... thumbnail 1 summary

Assignment Operator in C++ (=)



 

Assignment operator is use to assign value to variable or assign the value of one variable to another variable. For example we have two variable int a and int b when want to assign values to the variable so we use assignment operator like this
a=5;
b=3;

We can also use assignment operator to assign value to more than on variable in a single step for

example.
a=b=c=10;
a=b=20;
kown if we want to assign the value of variable a to the variable b so we use the assignment operator like this
int a;
int b;
int c;
c=a;
a=b;
b=c;
So the value of variable b will assign to variable a and the value of variable a will assign to variable b. and the final result will display a=5 and b=4.
Example1
#include<iostream.h>
int main()
{
int a=10;
int b=20;
c=a;
a=b;
b=c;
cout<<a;                 
cout<<b;
system("pause");
return 0;
};
Output: 20, 10 
Example 2
#include<iostream.h>
int main()
{
int a=10;
int b=20;
int c=30;
int t;
t=a;
a=c;
c=b;
b=t;
cout<<a;                     
Cout<<b;    
cout<<c;    
system("pause");
return 0;
};
Output: 30, 10, 20