- A variable can be defined as a memory location declared to store any kind of data (which may change many times during program execution).
- It can also be defined as any quantity/entity which may vary during program execution.
- Variables are the identifiers of a memory location.
- To identify a variable (the declared memory location), it can be assigned a name – known as a variable name.
- The name given to a variable is known as an identifier.
- An identifier can be a combination of alphabets, digits, and underscore.
Syntax
datatype Variablename;
Examples
- int mark;
- float a,b,result;
Variable naming rules
- It should be unique but not a keyword.
- The first letter should be an alphabet.
- White space, symbols, special characters are not allowed.
- Underscore can use ( _ ).
- Uppercase and lowercase letters are evaluated differently.
Variable initialization
Syntax
datatype variablename = size;
Examples
- int sum=10;
- int result=0, mark, sub=10;
- const float pi=3.14;
- In the first example the variable 'sum' is assigned with a value 10,which is called variable initialization.
- In the second example, the variable 'result' is assigned with a value 0, the variable 'sub' with a value 10.but the variable 'mark' indicates a normal variable declaration. In the third example, the variable pi declares as a constant with value 3.14, which cannot be altered during the program run.