Preprocessor Directives

C Preprocessor

In a C program, all lines that start with # are processed by the preprocessor, a special program invoked by the compiler.

In simple terms:

The preprocessor takes a C program and produces another C program where all preprocessor directives (# lines) are handled and removed.


Types of Preprocessor Directives

There are four main types of preprocessor directives:

  1. Macros

  2. File Inclusion

  3. Conditional Compilation

  4. Other Directives


Important Preprocessor Directives

All preprocessor commands:

  • Begin with #

  • Must be the first non-blank character on a line

  • Typically start in the first column for readability

List of Common Directives

Sr. No    DirectiveDescription
1    #define        Substitutes a preprocessor macro
2    #include        Inserts a header file
3    #undef        Undefines a macro
4    #ifdef        True if macro is defined
5    #ifndef        True if macro is not defined
6    #if        Tests a compile-time condition
7    #else        Alternative for #if
8    #elif        Combination of #else and #if
9    #endif        Ends conditional block
10    #error        Prints error message
11    #pragma        Issues compiler-specific instructions

Important Facts About the Preprocessor


1️⃣ File Inclusion (#include)

When using #include, the contents of the included file are copied into the current file after preprocessing.

Using Angle Brackets

#include <math.h>
  • Searches in standard system directories

Using Double Quotes

#include "yourheaderfile.h"
  • Searches in the current directory first

  • Then in system directories


2️⃣ Macros (#define)

When using #define, the preprocessor replaces matching tokens with the defined value.

Example: Constant Macro

#include <stdio.h> #define max 100 int main() { printf("max is %d", max); return 0; }

Output:

max is 100

⚠️ Note: The word max inside double quotes ("") is not replaced.


Example: Function-like Macro

#include <stdio.h> #define MULTIPLY(a, b) a*b int main() { printf("%d", MULTIPLY(2, 3)); return 0; }

3️⃣ Conditional Compilation

Preprocessors support conditional compilation using #if, #ifdef, #ifndef, etc.

Example:

#ifndef MESSAGE #define MESSAGE "You wish!" #endif

Platform-Specific Compilation Example

#ifdef __unix__ #include <unistd.h> #elif defined _WIN32 #include <windows.h> #endif
  • __unix__ → Defined on Unix systems

  • _WIN32 → Defined on Windows systems

Most Windows compilers define _WIN32.
Some define WIN32.

You can define macros manually during compilation:

-D_WIN32

4️⃣ Header File Multiple Inclusion Problem

If a header file is included multiple times, it may cause:

  • Redefinition errors

  • Duplicate declarations

Solution: Use Include Guards

#ifndef HEADER_FILE #define HEADER_FILE // header content #endif

5️⃣ Standard Predefined Macros

C provides some built-in macros:

MacroDescription
__FILE__        Current file name
__DATE__        Compilation date
__TIME__        Compilation time
__LINE__        Current line number

Example:

#include <stdio.h> int main() { printf("Current File : %s\n", __FILE__); printf("Current Date : %s\n", __DATE__); printf("Current Time : %s\n", __TIME__); printf("Line Number : %d\n", __LINE__); return 0; }

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays