VARIABLE
Definition
- It is a name of storage space which is used to store data.
- It's value is changeable.
- It always contains last value stored to it.
- It is always declared with data type.
Variable declaration
int rollno;
float marks;
char grade;
Here rollno is a variable of type int, marks is a variable of type float and grade is a variable of type char.
Variable Initialisation
int rollno=354;
float marks=74.4;
char grade='R';
Here 354 is the value of rollno, 74.4 is the value of marks and R is the value of grade. Character value is always written in single quotes.
Rules to declare a variable
- The first letter of a variable should be alphabet or underscore( _ ).
- The first letter should not be digit.
- After first character it may be a combination of letter or digits.
- Blank spaces are not allowed in variable name.
- Variable name should not be a keyword.
CONSTANT
Definition
- An element of program whose value can not be changed at the time of execution of program is called constant.
- It is also called literals.
- It may be int, float and character data type.
Rules for constructing integer constant
- It must have atleast one digit.
- It must not have decimal point.
- It may no be positive or negative.
- The range of integer constant is between -32768 to +32767.
- No comma or blank space are allowed in integer constant.
Rules for constructing floating point constant
- It must have atleast one digit.
- It must have a decimal point.
- It may be positive or negative.
- No comma or blank space are allowed in floating point constant.
Rules for constructing character constant
- It is a single alphabet, digit or special symbol.
- The length of character constant is 1 character.
- Character constant is enclosed within single qoutes (example:- 'A').
Use of constants in program
There are two ways of using constants in the C program.
- Using const
- Using #define
Example
int rollno=354;
float marks=74.4;
char grade='R';
Here 354 is integer constant, 74.4 is float constant and R is character constant.