Constant In C
Constants are entity which can not be changed after declaration in C.
How To Declare Constant?
1. Use const keyword in front of the variable.
#include<stdio.h> int main(){ const int a=5; printf("The value of a is: %d",a); return 0; }
Output:
5
2. Use define keyword in front of the variable.
#include <stdio.h> #define a 5 void main() { printf("%d",a); }
Output:
5
Types of Constant In C
- Integer Constant 100, 200, 450 etc.
- Floating-point Constant 20.3, 10.2, 850.6 etc.
- Octal Constant 921, 833, 026 etc.
- Hexadecimal Constant 0x5a, 0x3b, 0x4a etc.
- Character Constant ‘a’, ‘b’, ‘c’ etc.
- String Constant “hello world”, “c program”, etc.
You can’t change the value of a constant.
#include<stdio.h> int main(){ const int a=5; a=7; printf("The value of a is: %d",a); return 0; }
This will give error.