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.
Used to include a file in the source code
#include
program.
Used to include a section of code if a
#ifdef
certain macro is defined by #define.
Used to include a section of code if a
#ifndef
certain macro is not defined by #define.
#if Check for the specified condition.
Alternate code that executes when #if
#else
fails.
Used to mark the end of #if, #ifdef, and
#endif
#ifndef.
Used to generate a compilation error
#error
message.
Used to modify line number and filename
#line
information.
To make sure the header is included
#pragma once
only once.
Preprocessor Directives Description
Used for displaying a message during
#pragma message
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
#include <stdio.h>
#define PI 3.14159 // Defining a macro for PI
int main(){
// Using the PI macro to calculate
Double r = 8.0;
double area = PI * r * r;
printf("%f\n", a);
return 0;
}
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:
#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
// Including standard input output library
// using its header file
#include <stdio.h>
int main(){
// Using standard library functions
printf("Hello, World!\n");
return 0;
}
Output
Hello, World!
#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:
#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
#include <stdio.h>
int main() {
#define DEBUG
// Check if DEBUG is defined
#ifdef DEBUG
printf("Debugging is enabled");
#endif
return 0;
}
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:
#include <stdio.h>
int main() {
// Check if DEBUG is not defined
#ifndef DEBUG
printf("Debugging is not enabled");
#endif
return 0;
}
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
#include <stdio.h>
// Define macro that prints current line number and filename
#define PrintLineNum printf("Line number is %d in file named %s\n",
__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;
return 0;
}
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
#include <stdio.h>
#ifndef ABC
// Show error message
#error "ABC not found!"
#endif
int main() {
printf("Hello, ABC!\n");
return 0;
}
Output
error: #error "ABC 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
#include <stdio.h>
// not defining ABC to trigger pragma message
// #define ABC
int main(){
#ifndef ABC
#pragma message(" ABC is not defined.")
#endif
printf("Hello ABC!\n");
return 0;
}
Output
#pragma message: ABC is not defined.