C Constants

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Constants?

A constant is a value that does not change. C offers literal constants (42), the preprocessor (#define), the const qualifier, and enum for integer names. Prefer const or enum when you want type checking and debugger names.

Remember
#define MAX_USERS 100        /* text substitution */
const double PI = 3.14159;  /* typed, read-only object */
enum { SIZE = 8 };           /* integer constant */

Cheat Sheet

Keep these facts handy while you work with Constants in C.

TopicDetail
#defineNo type, no semicolon
constTyped, cannot be assigned after init
enumInteger constants usable as array sizes
Literal suffix10L, 3.5f, 7u

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Data Types.

Example 1 — const, #define, and enum together

Three ways to name a fixed number.

C
#include <stdio.h>

#define MAX_USERS 100

enum { COLS = 4 };

int main(void) {
    const double PI = 3.14159;
    double radius = 2.0;
    int grid[COLS] = {1, 2, 3, 4};

    printf("limit: %d\n", MAX_USERS);
    printf("area: %.2f\n", PI * radius * radius);
    printf("cols: %d, last: %d\n", COLS, grid[COLS - 1]);
    return 0;
}

How It Works

  1. MAX_USERS is replaced by 100 before compilation.
  2. PI is a real object; assigning to it is a compile error.
  3. COLS is an enum constant, so it can size an array.

Edge Cases & Pitfalls

Check these before relying on Constants in real programs.

macro

Semicolon after #define

#define N 10; makes every N expand to 10;, which breaks expressions such as N + 1.

const

const is not always a constant expression

In C a const int is not a constant expression, so it cannot size a global array or appear in a case label. Use enum or #define there.

Key Takeaways

  • Prefer: const or enum over raw numbers.
  • Macros: no type checking, no semicolon.
  • Names: UPPER_CASE signals a constant.

One line: A constant is a value that does not change.

Frequently Asked Questions

Use const (or enum) for typed values you want the compiler to check. Use #define for conditional compilation, header guards, and macros that need text substitution.
Casting away const and writing through the pointer is undefined behavior if the object was defined const.
Continue with "C Data Types" next. int, char, float, double, and the size and range of each type. The C Fundamentals hub lists every topic in order.

Next: Data Types

int, char, float, double, and the size and range of each type.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful