VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-preprocessor-directives-set-2/

⇱ C Preprocessor Directives - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Preprocessor Directives

Last Updated : 29 May, 2026

The C preprocessor is a program that processes the source code before actual compilation begins. It handles special instructions called preprocessor directives, which guide the compiler on how to prepare the code.

  • Preprocessor directives start with the # symbol
  • They are executed before compilation
  • They are used for macros, file inclusion, and conditional compilation
👁 Preprocessor-In-C
C Preprocessor Directives

Preprocessor Directives in C

#define

The #define directive is used to define macros or symbolic constants. These values are replaced before compilation.

  • Used to define constants and macros
  • Improves code readability
  • No memory allocation happens

Output
201.061760

#include

The #include directive is used to include header files in a program before compilation.

  • Allows use of library functions
  • Supports reusable code
  • Comes in two forms: < > and " "

Syntax

#include <filename>

#include "filename"

  • < > -> system header files
  • " " -> user-defined header files

Output
Hello, Geek!

Conditional Compilation Directives (#if, #elif, #else, #endif)

These directives work together to control which parts of the program get compiled based on certain conditions.

  • If the condition after the #if is true, the lines after it will be compiled.
  • If not, it checks the condition after associated #elif. If that's true, those lines will be compiled.
  • If neither condition is true, the lines after #else will be compiled.

Output
Value is less than or equal to 2

Note: the entire structure of #if, #elif and #else chained directives ends with #endif.

#ifdef

The #ifdef (if defined) directive checks whether a macro is defined. If it is, the code within the #ifdef block is included in the program.


Output
Debugging is enabled

#ifndef

The #ifndef (if not defined) directive checks if a macro is not defined. If it is not defined, the code inside the #ifndef block is included.


Output
Debugging is not enabled

#line

The #line directive is used to change line number and file name for the compiler.

  • Useful in generated code
  • Helps in debugging
  • Alters compiler reporting

Output
Line number is 5 in file named ./Solution.c
Line number is 20 in file named main.c
Line number is 30 in file named index.c

Line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on. "filename" - optional parameter that allows to redefine the file name that will be shown.

#error

The #error directive generates a compilation error with a custom message.

  • tops compilation immediately
  • used for enforcing conditions


Output

error: #error "GeeksforGeeks not found!"

#pragma - Pragma Directive

The #pragma directive gives special instructions to the compiler.

  • Compiler-specific behavior
  • Used for optimizations and warnings

Commonly used pragma directives are:

  • #pragma message: used at compile time to print custom messages.
  • #pragma once: to guard the header files i.e the header file must be included only once.
  • #pragma warning: to enable or disable the warnings.


Output

#pragma message: GfG is not defined.

Summary of Common Preprocessor Directives in C

Preprocessor Directives

Description

#define

Used to define a macro.

#undef

Used to undefine a macro.

#include

Used to include a file in the source code program.

#ifdef

Used to include a section of code if a certain macro is defined by #define.

#ifndef

Used to include a section of code if a certain macro is not defined by #define.

#if

Check for the specified condition.

#else

Alternate code that executes when #if fails.

#endif

Used to mark the end of #if, #ifdef, and #ifndef.

#error

Used to generate a compilation error message.

#line

Used to modify line number and filename information.

#pragma once

To make sure the header is included only once.

#pragma message

Used for displaying a message during compilation.

Comment