×
>
<

Aptitude

C Pre-Processors | CrackEase

Pre-Processors in C

C preprocessor illustration

What is a Pre-Processor in C?

The C preprocessor runs before the compiler proper. It scans the source file for preprocessor directives (lines beginning with #) and performs textual transformations such as file inclusion, macro substitution, conditional compilation and other source-level operations.

Important things to know:

  • The preprocessor works by simple textual replacement — it does not parse C syntax.
  • Preprocessor directives are evaluated before compilation and can enable/disable code, create constants, or inject code from header files.
  • Using preprocessor features correctly helps make code portable, configurable, and more maintainable.
Common directives
preprocessor types
  • #include: inserts the contents of another file (commonly header files) at that point in the source.
  • #define: creates symbolic constants or simple parameterless macros (textual substitution).
  • #ifdef, #ifndef, #endif: perform conditional compilation depending on whether an identifier is defined.
  • #undef: removes a previous macro definition.
  • #if, #elif, #else: conditional compilation using constant integer expressions.
  • #pragma: implementation-specific instructions to the compiler (nonportable; behavior depends on compiler).
Example: #include
            
#include <stdio.h>

int main(void)
{
    printf("Hello, world!\n");
    return 0;
}
            
        
Output
            
Hello, world!
            
        
Example: #define (constant)
            
#include <stdio.h>
#define PI 3.14159265358979323846

int main(void)
{
    float radius = 5.0f;
    float area = PI * radius * radius;
    printf("Area of circle: %f\n", area);
    return 0;
}
            
        
Output
            
Area of circle: 78.539825
            
        
Example: conditional compilation (#ifdef/#ifndef)
            
#include <stdio.h>

#define DEBUG

int main(void)
{
#ifdef DEBUG
    printf("Debugging is enabled.\n");
#else
    printf("Debugging is disabled.\n");
#endif

    return 0;
}
            
        
Output
            
Debugging is enabled.
            
        
Example: #undef (removing a macro)
            
#include <stdio.h>

#define MESSAGE "Hello, world!"
#undef MESSAGE

int main(void)
{
    /* MESSAGE is no longer defined. Uncommenting the next line will cause a compile error.
       printf("%s\n", MESSAGE); */
    printf("MESSAGE is undefined, so using a literal instead.\n");
    return 0;
}
            
        
Output
            
MESSAGE is undefined, so using a literal instead.
            
        
Example: #if / #elif / #else
            
#include <stdio.h>
#define VERSION 2

int main(void)
{
#if VERSION == 1
    printf("This is version 1.\n");
#elif VERSION == 2
    printf("This is version 2.\n");
#else
    printf("Unknown version.\n");
#endif

    return 0;
}
            
        
Output
            
This is version 2.
            
        
Example: #pragma (compiler-specific)
            
#include <stdio.h>

int main(void)
{
#pragma message("Compiling main function...")
    printf("Hello, world!\n");
    return 0;
}
            
        
Output
            
Hello, world!
            
        

Note: #pragma is implementation-defined. Different compilers support different pragmas; check your compiler's documentation.

Footer Content | CrackEase