Constant
Constant generally known as “const” keyword in programming language. It can assign a constant value in a variable.
Syntax:
const data_type variable_name=value;
Look at the program given below:
#include<stdio.h> void main() { const int a=0; printf("a=%d",a); return 0; }
OUTPUT
a=0
If you assign a new value in a variable, it will erase the old value and save the new value. You can change variable value as many as you want. But if you want to prevent to modify the value, you should use const keyword. If you use const keyword, you can’t change the value of a variable. For example:
#include<stdio.h> void main() { const int a=0; a=10; printf("a=%d",a); return 0; }
This program will show an error massage. Because you have used const keyword and changed the value of ‘a’ in fifth line of the program.
[Note: you must initialize const variable during declaration.]
Next Previous