Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Preprocessor and 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;
}
Try it Yourself »

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;
}
Try it Yourself »

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


#ifdef and #ifndef - Conditional Compilation

You can use #ifdef and #ifndef to compile parts of the code only if certain macros are (or are not) defined:

Example

#define DEBUG

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

This is useful for debugging or building different versions of the same program.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.