Skip to main content

Constants in C

A constant is a fixed value that cannot be changed during the execution of a program. It is also called a read-only value. Constants must be initialized at the time of declaration; otherwise, they may hold garbage values.

Ways to Define Constants

1. Using the const keyword

const data_type var_name = value;
const float PI = 3.14;

2. Using #define (Preprocessor Macro)

#define CONSTANT_NAME value
#define PI 3.14

Examples

Using const:
#include <stdio.h>
int main() {
    const float PI = 3.14;
    printf("Value of PI: %.2f\n", PI);
    return 0;
}
Using #define:
#include <stdio.h>
#define PI 3.14
int main() {
    printf("Value of PI: %.2f\n", PI);
    return 0;
}
Both produce the same output: Value of PI: 3.14

Attempting to Change a Constant

const int a = 10;
a = 20;   // ❌ Error: assignment of read-only variable 'a'

const vs #define

Featureconst#define
Handled byCompilerPreprocessor
Memory allocatedYesNo
Type safetyEnforcedNot enforced
ScopeFollows block rulesFile-wide only
Error checkingCompile-timeNone
DebuggingEasier — has a nameHarder — replaced before compiling
Best practice: Prefer const over #define for typed constants in modern C. Use #define for values that are truly compile-time substitutions (like include guards).