Back to Blog

C in 100 Seconds: Constants and Macros | Episode 14

Daryl WongDaryl Wong

Video: C in 100 Seconds: #define const enum — When to Use Each | Episode 14 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Constants and Macros — Immutable Values in C

C in 100 Seconds, Episode 14


Some values should never change. C gives you three ways to enforce that: #define, const, and enum.

#define Macros

#define PI 3.14159
#define MAX 100

#define is a preprocessor directive. Before compilation even starts, the preprocessor replaces every occurrence of PI with 3.14159 throughout your code. It's a text substitution — no type, no memory allocation.

const Variables

const int year = 2026;

const creates a real variable that the compiler prevents you from modifying. Unlike #define, it has a type and takes up memory. The compiler can catch mistakes if you try to reassign it.

Enums

enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
printf("Color = %d\n", c);  // 1

Enums assign sequential integer values starting from 0. RED is 0, GREEN is 1, BLUE is 2. They give meaningful names to related integer constants.

#define vs const

#define const
Type checking No Yes
Memory No allocation Allocated
Scope Global (text replacement) Follows normal scoping
Debugging Replaced before compile Visible in debugger

Use const when you want type safety. Use #define for compile-time constants that need to work across different contexts (like array sizes in older C standards).

Full Code

#include <stdio.h>

#define PI 3.14159
#define MAX 100

enum Color { RED, GREEN, BLUE };

int main() {
  const int year = 2026;

  printf("PI = %f\n", PI);
  printf("MAX = %d\n", MAX);
  printf("Year = %d\n", year);

  enum Color c = GREEN;
  printf("Color = %d\n", c);

  return 0;
}

Compile and Run

gcc constants.c -o constants
./constants

Next episode: Type Casting — converting between types explicitly.

Student code: github.com/GoCelesteAI/c-in-100-seconds/episode14