🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #94

C Macros


Preprocessor and Macros

In C, the preprocessor runs before the actual compilation begins. It handles things like including files and defining macros.

Preprocessor commands begin with a # symbol and are called directives.


#include - Include Header Files

You have already seen the #include directive many times - It tells the compiler to include a file.

It is used to add libraries or custom header files:

Example

#include <stdio.h>
#include "myfile.h"

Use angle brackets < > for standard libraries and double quotes " " for your own files.

Tip: The most commonly used libraries can be found in our C Reference Documentation.


#define - Create a Macro

A macro is a name that represents a value (like PI), or a piece of code, defined using the #define directive.

In the example below, PI is replaced with 3.14 before the program is compiled.

This means that every time PI appears in the code, it will be replaced with 3.14:

Example

#define PI 3.14

int main() {
  printf("Value of PI: %.2f\n", PI);
  return 0;
}

Macros can also take parameters, like a function:

Example

#define SQUARE(x) ((x) * (x))

int main() {
  printf("Square of 4: %d\n", SQUARE(4));
  return 0;
}

Macros with parameters work like shortcuts, but be careful with parentheses to avoid mistakes.


#ifdef and #ifndef - Conditional Compilation

The #ifdef and #ifndef directives let you include or skip parts of the code depending on whether a macro is defined.

This is called conditional compilation, and it's useful for debugging or creating different versions of a program.

Example

#define DEBUG

int main() {
  #ifdef DEBUG
    printf("Debug mode is ON\n");
  #endif
  return 0;
}

If DEBUG is defined, the message will be printed. If it's not defined, that part of the code is skipped.


Create Your own Header Files

In the next chapter, you will learn how to create your own header files and organize your code across multiple files using "modular programming".

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Macros – FAQs

Quick answers about learning C Macros in C.

This free note from CodingNow 2.0 explains C Macros in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Macros, is 100% free with no signup required.
With focused practice, most students grasp C Macros in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now