Detailed Explanation of Constants in C Language

Constants are values or variables in a program that cannot be changed, such as: 10, 20, ‘a’, 3.4, “c programming”, etc. There are different types of constants in C programming.
The list of constants in C is as follows:

Detailed Explanation of Constants in C Language

Two Ways to Define Constants in C

In C programming, there are two ways to define constants.

👇 Click to Claim👇

👉 Collection of C Language Knowledge Materials
1. Using the const Keyword
The const keyword is used to define constants in C programming.
const float PI = 3.14;
Now, the value of the PI variable cannot be changed.
#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
If you try to change the value of PI, an error will occur at compile time.
#include <stdio.h>
int main(){   const float PI = 3.14;   PI = 4.5;   printf("The value of PI is: %f", PI);   return 0;}
Output:
Compile-time error: Cannot modify const object
2. Using the #define Preprocessor

The #define preprocessor is also used to define constants; the content about the #define preprocessor directive will be learned in later chapters.

Programmer Technical Exchange Group

Scan the code to join the group and remember to note: city, nickname, and technical direction.



Leave a Comment