Constant In C

Constant In C

Constants are entity which can not be changed after declaration in C.
Constant 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


  1. Integer Constant 100, 200, 450 etc.
  2. Floating-point Constant 20.3, 10.2, 850.6 etc.
  3. Octal Constant 921, 833, 026 etc.
  4. Hexadecimal Constant 0x5a, 0x3b, 0x4a etc.
  5. Character Constant ‘a’, ‘b’, ‘c’ etc.
  6. 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.
Load comments