Variables are simply names used to refer to a location in memory – a location that holds a value with which we are working. A particular type of a variable can hold only the same type of constant. For Example, an integer variable can hold only an integer constant and float variable can hold only float(real) constant. Each variable needs a unique Identifier(unique name to use it).
Rules for Constructing an Identifier
- An identifier is combination of 1 to 31 alphabets, digits or underscores. Some compilers allow identifiers length up to 247 characters but we should use the identifiers length up to 31 characters.
- An identifier must start with an alphabet or underscore.
- Symbols other than underscore is not allowed in an identifier.
- Keyword cannot be used as identifier.
Examples :
marks_1
amount
time_duration
rate
Variable Declaration in C
Before using a variable first we have to declare that variable.
Syntex :
type variable_name;
- int keyword is used to declare an Integer Variable.
- float keyword is used to declare an float(real number) Variable.
- char keyword is used to declare an Character Variable.
Examples:
int a; float f; char c;
Multiple variables can be declared in a single statement.
int i,j,k; float a,b; char c,d,e;
Storing values in variables
- Assignment Operator(=) is used to assign value to a varible.
int a; float b; char c; a = 20; b = 40.67; c='R';
- We can also assign value to a variable while declaration.
int a = 20; float b = 40.67; char c = 'R';