Defining Constants in C

There are two ways to define constant in C programming language :

1. const Keyword
2. #define preprocessor

1. const keyword :

The constant keyword is used to define the constant is C programming. We can use const prefix to declare constants with a specific data type :
const datatype variable_name = value;
Example :
const float PI = 3.14;
Now lets see an example c program :
#include <stdio.h>

int main() {
  const float PI = 3.14;
  const int RADIOUS = 5;
  const char NEWLINE = '\n';
 
  area = PI*(RADIOUS*RADIOUS);
 
  printf("Area of Square : %f", area);
  printf("%c", NEWLINE);
  return 0;
}
Output :

Area of Square : 78.50

Also note that in good programming practice the constants are defined with names within capital letters.

2. #define preprocessor :

The #define preprocessor is also used to define constant. The syntax for defiing constant with #define is :
#define identifier value
Example :
#define PI 3.14
Now lets see an example c program :
#include <stdio.h>

#define PI 3.14
#define RADIOUS 5

int main() {
  float area;
  area = PI*(RADIOUS*RADIOUS);
 
  printf("Area of Square : %f\n", area);
  return 0;
}
Output :

Area of Square : 78.50


Next Topic :