Constants in C programming language

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.

There are two simple ways in C to define constants :
  • Using #define preprocessor

  • Using const keyword

Given below is the form to use #define preprocessor to define a constant :
#define identifier value

Let's see an example of #define to define a constant.
#include<stdio.h>
#define PI 3.14
int main()
{
      printf("%f",PI);
}

Output :
3.140000


Given below is the example of const to define a constant :

#include<stdio.h>
int main()
{
      const float PI=3.14;
      printf("The value of PI is = %f",PI);
      return 0;
}

Output :
The value of PI is = 3.140000

0 Comments