Variables in C++

Variables is a name given to memory location. 

            int x =10;                    

here, x is a integer variable.                                                    

Integer variable x is a name given to memory location and where we stored integer value10.

Initialize variable

we can also initialize variables as below.

int  a,b,c;

a=10;
b=20;
c=30;

Here, we have initialized three integer variable a,b & c. variable  ‘a’  stores 10, variable ‘b’ stores 20 and variable ‘c’ stores 30.

Initialize multiple variable

we can also initialize multiple by comma separated list.

int a;             
char b;       
float c;
a=10;
b=’S’;
c=11.23

How to take input from user in C++

In C++ we can take a input from user by using “cin”.

In C++ , “cin” is an  object of class istream. 

“cin” object  is used to take the input from the standard input device i.e. keyboard.

In “cin” ,   “c” refers to “character” and ‘in’ means “input”, hence cin refers “character input”.

“cin” is used with the extraction operator (>>) for receive a stream of characters from user.

Syntax  of cin is:

cin>>variableName;

Example

#include 
using namespace std;
int main()
{
	int x;

	/*For single input */
	cout << "Enter a number: ";
	cin >> x;	//  take input from user and initialize variable x

	cout << "\n x = " << x;
	return 0;
}
Enter a number:    5  //  5 enter by user and it’ll store in variable x
x = 5

How to take multiple input from user in C++

In C++ we can also take a multiple input at a time by using cin object.

Syntax to take multiple input at time by using cin.

cin >> Variable1 >> Variable2 >> … >> VariableN;

Example:

 
#include 

 using namespace std;
 int main()
 {
 	int x, y, z;
 	/*For multiple input */
 	cout << "Enter 3 number: ";
 	cin >> x >> y >> z;	// three numbers enter by user using cin

 	cout << "\n x = " << x;	// print value of x using cout
 	cout << "\n y = " << y;	// print value of y using cout
 	cout << "\n z = " << z;	// print value of z using cout
 	return 0;
 }

Output:

Enter 3 number: 5		// first number enter by user
10				// second  number enter by user
20				// third number enter by user
x = 5
y = 10
z = 20
Categories C++