As the name suggest, a variable is a quantity which varies during a program execution. In C + +, a variable is 0 location where the information is stored. This location exist in a computer memory where the value information can stored and a case of need it can be retrieved.
Depending upon the size of variable, it can occupy the one more memory address.
Variable can be change in any combination of letter without spaces. Variable may be short or descriptive but descriptive variable names makes it easier to understand the flow of program some keyword are reserved in C + + and cannot be used as variable, name like – if, while, main for. We can create more than one variable of the same type in a single stmt this can be done by the following syntax:
Syntax
Type varname, varname; |
eg. int A; here Int is datatype od variable and A is variable name.
# include < iostream .h >
main ( )
{
int a, b, sum ;
clrscr( );
cout << “ \ Enter the first number” ;
cin >> a ;
cout << ‘ \ Enter the second number
cin >> b ;
Sum = a + b ;
cout << sum << end 1 ;
return 0 ;
}
Explanation:
1. A C + + program start with function called main ( ). The body of the function is enclosed between curly bracket ( {} ). A C+ + program consist of as many function as required.
2. The explanation written after the characters 11 is know as comment, comment play an important role in development of program. In case comment more than one line, it should be enclosed within symbols /* and */. There should be no space between / and * for example.
/* The program computes the perimeter of trapezium */
3. The statement in program # include
4. The function cin is called console input its used with redirectional operator >>. It accepts the data from the input unit i.e. keyboard cin >> a ;
When this statement is executed it accepts the value from keyboard.
5. The function coat is called consol output. Its used with redirection operator <<. It display the output on the screen
cout << sum << end 1;
When this statement is executed, it displays the value (sum) on the screen end 1; it used to show the end of the line. Its optional to use end 1.
End 1 is manipulator which frequently used. It has same effects as character h / nc1 due to clarity end 1 is preferred.
6. Every statement in C++ should be follow by a semi – colon ( ; ) In case this semi colon is missed, an error message appears.
The cin operator ( >> ) extraction operator:
The operator is used to get in put from the standard input devices i.e. keyboard. When you write a C + + program and used cin, you do not know the value of the input until the program is run. You can also fill the value using assignment stmt such as i = 13; the difference between assignment statement and cin operator is that value of the input is known is case of assignment statement where as it is not known in case of cin operator.
Syntax : cin >> values ;
Look at the following program :
// INTRO . CPP
# Include
main ( )
{
int a, b ;
cout << “ Enter one value = ”<< end 1;
cin >> a ;