Constants in C

In this tutorial, we will learn about Constants in C. Constants are fixed values. It means after defining a variable we can not change value. These fixed values are also called literals.

Defining Constants in C:

There are two simple ways in C to define constant −

  • Using #define preprocessor.
  • Using const keyword.

The #define Preprocessor

Please check out the following syntax to define a constants using a #define preprocessor.

#define identifier value

Please check out the following example program.

#include <stdio.h>
#define pi 3.14   

int main() {
   printf("Pi : %f", pi);
   return 0;
}

Output:

Pi : 3.140000   

The const keyword:

There is another way to create constants in C is using const keyword. Please check out the following example program.

#include <stdio.h>

int main() {
   const float pi = 3.14;
   printf("Pi : %f", pi);
   return 0;
}

Output:

Pi : 3.140000   

 

Spread the love
Scroll to Top
×