Open In App

C Preprocessor Directives

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.

List of Preprocessor Directives

The following table lists some commonly used preprocessor directives available in the C programming language:

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.

#define

In C, the #define preprocessor directive is used to define the macros and symbolic constants. The macros are the identifiers defined by #define which are replaced by their value before compilation.

Example

C
//Driver Code Starts{
#include <stdio.h>

//Driver Code Ends }

// Defining a macro for PI
#define PI 3.14159

int main(){
    
    // Using the PI macro to calculate
    double r = 8.0;
    10double area = PI * r * r;

//Driver Code Starts{
    
    printf("%f
", a);
    return 0;
}
//Driver Code Ends }

Output
201.061760

#include

#include preprocessor directive is used to include the contents of a specified file into the source code before compilation. It allows you to use functions, constants, and variables from external libraries or header files. There are two types:

C
#include <file_name>

#include "filename"

Here, file inclusion with double quotes ( ” ” ) tells the compiler to search for the header file in thedirectory of source filewhile <> is used for system libraries.

Example

C
//Driver Code Starts{

//Driver Code Ends }

// Including standard input output library
// using its header file
#include <stdio.h> 

//Driver Code Starts{

int main(){
  
    // Using standard library functions
    printf("Hello, Geek!
");

    return 0;
}
//Driver Code Ends }

Output
Hello, Geek!
Sum: 15

#if, #ifdef, #else, #elif, #endif

The above directives are Conditional Compilation directives that help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some conditions.

#if, #else and #elif

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.

Example:

C
#include <stdio.h>

int main() {
    #define VALUE 2  

    // Check if VALUE is greater than 3
    #if VALUE > 3  
        printf("Value is greater than 3\n");

    // Check if VALUE is 3 
    #elif VALUE == 3  
        printf("Value is 3");

    // If neither conditions are true, this
    // block will execute
    #else  
        printf("Value is less than or equal to 2");
    #endif

    return 0;
}

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.

Example

C
//Driver Code Starts{
#include <stdio.h>
int main() {
    
//Driver Code Ends }

#define DEBUG

// Check if DEBUG is defined
#ifdef DEBUG
    printf("Debugging is enabled");
#endif

//Driver Code Starts{

    return 0;
}
//Driver Code Ends }

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.

Example:

C
//Driver Code Starts{
#include <stdio.h>
int main() {

//Driver Code Ends }

// Check if DEBUG is not defined
#ifndef DEBUG
	printf("Debugging is not enabled");
#endif

//Driver Code Starts{
	return 0;
}

//Driver Code Ends }

Output
Debugging is not enabled

#line

The #line directive allows you to specify a line number and filename to be used by the compiler. This is often used in generated code or debugging.

Example

C
//Driver Code Starts{
#include <stdio.h>

//Driver Code Ends }

// Define macro that prints current line number and filename
#define PrintLineNum printf("Line number is %d in file named %s
", __LINE__, __FILE__)

int main() {
    PrintLineNum;

    // Change line number to 20 and filename to "main.c"
    PrintLineNum;

    // Change line number to 30 and filename to "index.c"
    #line 30 "index.c"
    PrintLineNum;

//Driver Code Starts{

    return 0;
}
//Driver Code Ends }

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. This is useful for detecting and reporting certain conditions during preprocessing. This immediately terminates the compilation.

Example

C
#include <stdio.h>

#ifndef GeeksforGeeks

// Show error message
#error "GeeksforGeeks not found!"
#endif

int main() {
    printf("Hello, GeeksforGeeks!\n");
    return 0;
}


Output

error: #error "GeeksforGeeks not found!"

#pragma – Pragma Directive

The #pragma directive is a special purpose directive that is used to turn on or off some features. #pragma also allows us to provide some additional information or instructions to the compiler. It is compiler-specific i.e., the behaviour of pragma directive varies from compiler to compiler.

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.

Example

C
#include <stdio.h>

// not defining gfg to trigger pragma message
// #define GeeksforGeeks

int main(){
#ifndef GeeksforGeeks
#pragma message(" GfG is not defined.")
#endif
    printf("Hello geek!\n");
    return 0;
}


Output

#pragma message:  GfG is not defined.


Next Article

Similar Reads