Definition:It is a data name which is used to store data and may change during execution. A variable is used to create and locate the data at a particular location in computer's memory. Variable is data item whose value can be changed during the exam execution.
syntax:
data_type variable_name;
Rules of variable name:
- Variable must start with a letter.
- Variable name can contain letters, numbers and the underscore.
- Keywords are not allowed to use as a variable name.
- Variable name can not have space in between.
- Variable name can not have special character.
Scope of variable:
A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all the functions. While a local variable is one declared within the body of a function or a block.
Declaration of variable:
C++ allows the declaration of a variable can be declared right at the place of its first use. This make the program much easier to write.
Example program:
int main()
{
int num1, num2, result; //variable declaration
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
result=num1+num2;
cout<<"\n Addition is:"<<result;
return 0;
}
OUTPUT:
Enter first number:2
Enter second number:3
Addition is:5
No comments:
Post a Comment