HOW TO DECLARE A CONSTANT IN C

Dev Frank
1 min readDec 23, 2023

--

Dev Frank

To declare a constant in your program you use the keyword “ const ’’.

For instance, if we write a program

void main()
{

const int a = 10;

a = 50; (this line of code will give
an error)
}

We have declared and initialized a constant in line 4 of our program with data type “ int ’’ and a variable “ a ’’ to be 10.

So declaring and initializing a constant int a = 10 cannot be changed or overwritten during your program, like in line 6.

Line 6 will give error because we declared a constant in line 4 and we are trying to overwrite it in line 6.

But if you didn’t declare a constant in line 4 ( removing the const ), line 6 will be valid.

You can also declare a symbolic constant using macro definition.

#define PI 3.14
#define MAX 10

NB: There should not be a whitespace after the “ # ’’. The symbolic constant must be in capital letter to differentiate from the normal constants.

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.