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:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share




Great News! Now you can read all the tutorials on our Android App..

Download Owlbuddy: Learn Programming Tutorials App Now.

Download Owlbuddy: Learn Programming Tutorials