Variable is name of memory location which contain value of specific type.
Value of variable can be changed during execution of program.
Fixed values which do not change during execution of program are called constants . constants in c can be categorized in following types.
1. Decimal Integer constants:
const int i=12;
%d or %i format specifier is used to print decimal integer value with in printf function.
2. Octal integer constants:
const int i=012;
Any constant preceded by 0 is regarded as octal integer constant . To print octal value %o format specifier is used.
3. Hexadecimal constants:
const int i=0x12;
Any constant preceded by 0x or 0X is regarded as Hexadecimal Integer constant. To print Hexadecimal value %x format specifier is used in printf function.
4. Real constants:
const float a=3.14;
We can use %f,%e or %E format specifier within printf function to print real values.
5. Character constants:
const char ch=’d’;
Any constant enclosed in single quotes is character constants. %c format specifier is used to print single character .
6. String constants
Constant enclosed in double quotes is called String constant. We can use %s format specifier to print string value.
const int i=12;
const char ch='z';
const char name="Ravin";
7. Escape Sequence:
Escape sequence is special character constant Which consistof two character and used with output function to print invisible pattern like newline and backspace
List of escape sequence :
‘\b’: form feed
‘\f’: New line
‘\r’: carrige return
‘\t’: horizontal tab
‘\”: to print single quotes
‘\\’:to print back slash
‘\”‘: To print double quotes
If you try to reassign a value to constant , you will encounter compile time error saying “Cannot modify a const object”. For example see code below:
const int i=12; printf("%d",i); i=15; printf("%d",i);