C #define Directive

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is #define Directive?

#define NAME replacement makes the preprocessor replace every later NAME token with replacement. It can define constants, expressions, and function-like macros. Use #undef NAME to remove it. Names are conventionally written in UPPER_CASE.

Remember
#define PI 3.14159
#define BUFFER_SIZE 64
#define SQUARE(x) ((x) * (x))
#undef BUFFER_SIZE

Cheat Sheet

Keep these facts handy while you work with #define Directive in C.

TopicDetail
Constant#define N 10
Function-like#define F(x) ...
Multi-lineEnd each line with \
Remove#undef NAME

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Object-like Macros.

Example 1 — Constants for a circle

The same macro is used in an array size and a formula.

C
#include <stdio.h>

#define PI 3.14159
#define BUFFER_SIZE 64

int main(void) {
    char buf[BUFFER_SIZE];
    double r = 5.0;

    printf("size=%zu area=%.2f\n", sizeof buf, PI * r * r);
    return 0;
}

How It Works

  1. BUFFER_SIZE becomes 64, so buf has 64 bytes.
  2. PI * r * r is 3.14159 * 5 * 5 = 78.53975.
  3. %.2f rounds to 78.54.

Edge Cases & Pitfalls

Check these before relying on #define Directive in real programs.

semi

Semicolon in the definition

#define N 10; expands to 10; and breaks expressions.

eq

Using = in the definition

#define N = 10 replaces N with = 10.

Key Takeaways

  • Text substitution only.
  • No semicolon.
  • Prefer const or enum for typed constants.

One line: #define NAME replacement makes the preprocessor replace every later NAME token with replacement.

Frequently Asked Questions

No. It is a text replacement performed by the preprocessor and occupies no memory at run time.
Yes. Use gcc -DDEBUG or -DSIZE=10 to define macros without editing the source.
Continue with "C Object-like Macros" next. Macros without parameters: constants, strings, and expressions. The C Preprocessor hub lists every topic in order.

Next: Object-like Macros

Macros without parameters: constants, strings, and expressions.

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.

10 people found this page helpful