Constants & Variables
Definition
- Constant is a number or a character representing a quantity, assumed to have fixed value.
- Variable is a name given to memory location for storing a value and its value may vary during program execution.
Description
Constants
Constants are divided into three types : Integer, Real and Character. Each constant have some rules for building it and these rules must be followed, while writing the program.
- Rules for Integer Constants
-
- Must have at least one digit without any decimal point.
- No commas or blank space are allowed.
- It could be either positive or negative, For no sign specification it is assumed to be positive.
Eg: 93 , +541 , -383
- Rules for Real constants
-
- Must have at least one digit with decimal point.
- No commas or blank space are allowed.
- It could be either positive or negative, For no sign specification it is assumed to be positive.
Eg: 48.63 , +38.73 , -382.44
- Rules for Character constants
-
- Maximum character length must be 1.
- It might be a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
Eg: 'a' , 'R', '4' , '='
Variables
Similar to constants, variables also have some rules for naming it.
- Name can contain any combination of alphabets, digits or underscores, but first character must be alphabet or underscore.
- No commas or blanks are allowed.
- Keywords cannot be used as a variable name.
For declaring a variable you need to specify its name, data-type it can handle and a semi-colon at the end of declaration. Four basic data-types of variables are int, char, float and double.
Eg: int i;
char event;
float value;
Now you can initialize or assign values to these variables:
Eg: int i=0;
char event='a';
float value=834.22;