C Preprocessor Directives
Last Updated :
02 Apr, 2025
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 }
#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 }
OutputHello, 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;
}
OutputValue 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 }
OutputDebugging 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 }
OutputDebugging 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 }
OutputLine 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.
Similar Reads
C Preprocessors
Preprocessors are programs that process the source code before the actual compilation begins. It is not the part of compilation, but a separate process that allows programmers to modify the code before compilation. It is the first process that the C source code goes through while being converted to
8 min read
C Preprocessor Directives
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 DirectivesThe fol
7 min read
How a Preprocessor works in C?
Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan
3 min read
Header Files in C
In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor. C language uses header files to provide the standard
6 min read
Whatâs difference between header files "stdio.h" and "stdlib.h" ?
In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used
4 min read
How to write your own header file in C?
As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar
4 min read
Macros and its types in C
In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content. Example [GFGTABS] C //Driver Code Starts{ #include <stdio.h
5 min read
Interesting Facts about Macros and Preprocessors in C
In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the â#â symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before
6 min read
# and ## Operators in C
In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs. St
3 min read
How to print a variable name in C?
Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification. Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with
1 min read