100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Constants in C

Understand C constants: literals, #define, const keyword, enumeration constants, and how to use each correctly.

Basics & Data TypesBeginner7 min readJul 7, 2026
Analogies

1. Introduction

A constant in C is a value that cannot be changed during program execution. Constants are used to represent fixed values like mathematical constants (e.g., PI), configuration limits, or flags, making code more readable, maintainable, and less error-prone than scattering 'magic numbers' throughout a program.

🏏

Cricket analogy: The LBW rule's height of stumps never changes mid-match, just as PI stays fixed in a program instead of being scattered as unexplained magic numbers across the scorecard code.

C offers several ways to define constants: literal constants embedded directly in expressions, the preprocessor #define directive, the const type qualifier, and enumeration constants declared with enum.

🏏

Cricket analogy: Just as a match can be defined by literal overs (20 for T20), a printed rulebook clause (#define), an umpire's fixed decision (const), or named categories like 'powerplay' (enum), C offers several ways to fix a value.

2. Syntax

c
#define CONSTANT_NAME value
const dataType variableName = value;
enum { CONST1, CONST2, CONST3 };

3. Explanation

#define is a preprocessor directive that performs a simple textual substitution before compilation begins — it does not create a real variable, has no type, and consumes no memory of its own; the preprocessor just replaces every occurrence of the name with its value. The const qualifier, by contrast, creates an actual typed variable whose value cannot be modified after initialization; it is type-checked by the compiler and obeys normal scope rules, making it generally safer and preferred in modern C.

🏏

Cricket analogy: #define is like the groundstaff painting a boundary rope marker before the match starts (text substitution beforehand, no lasting entity), while const is like a permanent stadium fence that's inspected and enforced by officials for the whole game.

Literal constants include integer constants (42, 0x2A, 052), floating-point constants (3.14, 2.5e3), character constants ('A'), and string literals ("hello"). Suffixes clarify literal types: 'L'/'l' for long, 'U'/'u' for unsigned, 'F'/'f' for float (e.g., 100UL, 3.14f). A const variable must be initialized at the point of declaration since it cannot be assigned afterward.

🏏

Cricket analogy: Denoting a score as 250runs vs 250balls is like C's suffixes: '100UL' tags a value as unsigned long the way a scorecard tags '250*' as not-out, and a const total must be set the moment the innings closes, not adjusted afterward.

Tip: Prefer 'const' or 'enum' over '#define' for typed constants in modern C — they integrate with the type system, respect scope, and are visible to the debugger, whereas #define macros are invisible after preprocessing.

4. Example

c
#include <stdio.h>

#define PI 3.14159

int main(void) {
    const int MAX_USERS = 100;
    const float TAX_RATE = 0.18f;
    int radius = 5;
    float area;

    area = PI * radius * radius;

    printf("PI = %.5f\n", PI);
    printf("Circle area = %.2f\n", area);
    printf("MAX_USERS = %d\n", MAX_USERS);
    printf("TAX_RATE = %.2f\n", TAX_RATE);

    /* MAX_USERS = 200; would cause a compile-time error */

    return 0;
}

5. Output

text
PI = 3.14159
Circle area = 78.54
MAX_USERS = 100
TAX_RATE = 0.18

6. Key Takeaways

  • Constants hold fixed values that cannot change after being set.
  • #define performs textual substitution at preprocessing time and has no type.
  • const creates a typed, scoped variable that is checked by the compiler.
  • A const variable must be initialized at declaration and cannot be reassigned later.
  • Literal suffixes (U, L, F) specify the exact type of numeric literals.
  • enum constants provide a readable way to define related sets of named integer constants.

Practice what you learned

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#ConstantsInC#Constants#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep