Constants in C Programming

In this post (Constants in C Programming), we will read about C Constants and learn how you can define and use constants in c language with the help of const keyword and #define preprocessor.

When we work on a big project in real life, then we also need some variables in our program whose value we want to keep fixed.

Now because the project would have been bigger, then due to continuous coding for many hours, we sometimes forget and change the value of those variables.

Due to which the work approach of our project gets messed up and to avoid this kind of problem or should I say fix values, we use constants instead of variables.

Constants in C Programming
Constants in C Programming

You can also read: Variables in C Programming

Constants in C – What is C Constants?

Before storing data in computer memory, we assign names (identifiers) to memory locations and we call these names as variables or constants.

The method of declaration of both Variables and Constants is different and from the declaration syntax itself it is known whether the variable will be created in memory or constant.

The work of Variables and Constants is same i.e. to store data in memory but there is a difference between them and you will understand that difference only from the meaning of these two.

Variable means that which can change at any time and Constant means that which can never change.

If understood from the point of view of C programming, then the value of variables can change anytime and anywhere in the program but the value of constants never changes.

If you accidentally change the value of any constants in your program, then you get an error at compile time itself so that you can fix it at compile time itself and at run time your program can run without any problem.

How to Define and Use Constants in C Language

In C language, you can define constants mainly in two ways. We will talk about both types of constants one by one and understand both with examples.

  • Using const keyword
  • Using #define preprocessor

Define constants using const keyword in C

Declaring constants in c programming with the help of const keyword is almost the same as variable declaration.

The difference is that you have to use the const keyword before the data type and you have to initialize the value to the constant at the time of constant declaration.

Out of both the methods of defining constants, defining constants with the help of const keyword is considered to be the better way.

Because with the help of const keyword you can define constants anywhere local or global, that is, you can control the scope of constants.

const keyword syntax:

const data_type constant_NAME = value;

const keyword example:

#include <stdio.h><stdio.h>
int main()
{
    const int SIZE = 125;
 
    printf("Value of SIZE Constant : %d",SIZE);
 
    return 0;
}</stdio.h>

Define constants using #define preprocessor in C

In C programming, you can also declare constants with the help of #define preprocessor and in this also there is no semicolon (;) in the last like #include statement.

When you declare constants in this way, then those constants do not get a place in the memory because because of the preprocessor directive, even before the program compile time, the IDE puts the value of the constant at the places where you have written the constant.

#define Constants Declaration Syntax:

#define constant_NAME value

#define Constants Declaration Example:

#include <stdio.h><stdio.h>
#define SIZE 125
int main()
{
    printf("Value of SIZE : %d",SIZE);
 
    return 0;
}</stdio.h>

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com .

You can also visit my Hindi YouTube Channel.

Leave a Comment