Defining Constants in C

Constants are fixed value variables, whose value cannot be altered throughout the execution of program. The fixed value is known as literal.

You can define a constant for any type. They behave like normal variables expect that they are readonly (once assigned cannot be modified).

C supports two styles of constant definition.

  1. Using const keyword
  2. Using #define directive

But before we learn to define constants, let us identify its need.

Need of constant variable?

To understand the need of a constant variable let us suppose a situation. Consider the below program to calculate area of circle.

#include <stdio.h>

int main()
{
    float radius, pi, area;
    pi = 3.14159;
    radius = 12;
    area = pi * radius * radius;

    printf("Area = %f", area);

    return 0;
}

In above program we declared and used pi as a normal variable. We can easily modify the value of pi anywhere in program. In bigger programs you may accidentally alter the value of pi which would be a nightmare for expressions dependent on pi. Since PI is a mathematical constant whose value is fixed.

Declaring constants restrict programmers to alter its value. The compiler looks for change in the constant variable and report errors if found.

Let us improve our program using a constant variable.

#include <stdio.h>

int main()
{
    const float PI = 3.14159;
    float radius, area;

    PI = 3.14; // <-- Will generate error, it must not be modified

    radius = 12;
    area = PI * radius * radius;
    printf("Area = %f", area);

    return 0;
}

This time the compiler detects modification in a constant variable and generates an error message Assignment of a read-only variable 'PI'.

Defining constant using const keyword

A variable declared with const keyword is marked as readonly. The compiler looks for modification of readonly variables and report errors if found.

Syntax to define constant using const

const <data-type> <constant-name> = <constant-value>;

Or

<data-type> const <constant-name> = <constant-value>;

Example to define constant using const

const float PI = 3.14159f;

float const e = 2.71828f;

Defining constant using #define

#define is a pre-processor directive used to define constants and macros. It defines compile time constant and guarantees 100% constantness.

Unlike const keyword it does not define any variable and doesn't consumes memory. Rather during the compilation process, the compiler replaces all occurrence of the defined constant with its literal value. Hence, it is also called as compile time constant.

Syntax to define constant using #define

#define <constant-name> <constant-value>

Example to define constant using #define

#include <stdio.h>
#define PI 3.14159 // <-- Define constant PI

int main()
{
    float radius, area;

    printf("Enter radius: ");
    scanf("%f", &radius);

    area = PI * radius * radius;

    printf("Area = %f", area);

    return 0;
}

#define PI 3.14159 defines a constant PI with value 3.14159. The pre-processor replaces all occurrence of PI with 3.14159 before compilation.

Important note: It has been a legacy and also considered as a good programming practice to declare constant variable names in ALL CAPS.

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>