Constants in C — How to define constants?

What are constants in C? Constants are mounted worth variables, whose worth can’t be altered all through the execution of the program. We name the mounted worth literal.

You possibly can outline a relentless for any sort. They behave like regular variables, except that they’re read-only (as soon as assigned can’t be modified).

Want of fixed variable?

To grasp the necessity of a relentless variable allows us to suppose a state of affairs. Take into account the beneath program to calculate the area of a 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;
#embody <stdio.h>

int essential()

In the above program, we declared and used pi as a traditional variable. We can simply change the worth of pi wherever in the program. In larger applications, you might by chance alter the worth of pi which might be a nightmare for expressions depending on pi. Since PI is a mathematical fixed whose worth is mounted.

Declaring constants prohibit programmers to change its worth. The compiler appears for change within the fixed variable and report errors if discovered.

Allow us to enhance our program using a relentless variable.

#embody <stdio.h>

int essential()

    const float PI = 3.14159;
    float radius, space;

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

    radius = 12;
    space = PI * radius * radius;
    printf(“Space = %f”, space);

    return 0;

This time the compiler detects modification in a relentless variable and generates an error message Project of a read-only variable ‘PI’.

To learn about Variables and Exprssions — Click Here

Defining fixed using const key phrase

A variable declared with const key phrase is marked as read-only. The compiler appears for modification of read-only variables and report errors if discovered.

Syntax to outline fixed using const

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


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

Instance to outline fixed using const

const float PI = 3.14159f;

float const e = 2.71828f;

Defining fixed using #outline

#outline is a pre-processor directive used to outline constants and macros. It defines compile-time fixed and ensures 100% constantness.

Not like const keyphrase, it doesn’t outline any variable and would not consume reminiscence. Reasonably throughout the compilation process, the compiler replaces all prevalence of the outlined fixed with its literal worth. Therefore, it is usually referred to as a compile-time fixed.

Syntax to outline fixed using #outline

#outline <constant-name> <constant-value>

Instance to outline fixed using #outline

#embody <stdio.h>
#outline PI 3.14159 // <-- Outline fixed PI

int essential()

#outline PI 3.14159 defines a relentless PI with worth 3.14159. The preprocessor replaces all prevalence of PI with 3.14159 earlier than compilation.

Essential be aware: It has been a legacy and in addition, thought about as a very good programming apply to declare fixed variable names in ALL CAPS.

Learn more at Teche Programmer — Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *