
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pre-Processor Directives in C Language
The pre-processor is a tool that processes the source code before it passes through the compiler. It work as an initial phase of compilation where it operates under the control of different command lines or directives.
Pre-processor Directives in C
Pre-processor is placed in the source program before the main line, it begins with the symbol "#" in column one and does not require a semicolon at the end.
The commonly used pre-processor directives are −
- #define
- #undef
- #include
- #ifdef
- #endif
- #if
- #else
The pre-processor directives are divided into three categories −
- Macro substitution directives.
- File inclusion directives.
- Compiler control directives.
Macro Substitution Directives
This helps us to define macros that act as placeholders or shortcuts for code or constants and expressions. The pre-processor scans the source code and replaces each appearance of macros with its corresponding text or value before compilation begins.
Syntax
Given below is the syntax for the macro substitution directive −
#define identifier string
Example
Following is an example of a simple macro −
// Following is a sample macro #define MAX 500 // Following is a macro with arguments #define sqrt(x) x*x // Following is a nested macro #define A 10 #define B A+1
With macro arguments, we can perform operations like calculations on input data directly, which makes the code more efficient.
File Inclusion Directives
File Inclusion is used for adding content of one file to another when its pre-processing. This helps us when we need to include external libraries or our own header files.
Syntax
Given below is the syntax for the file inclusion directive −
#include "filename" or, #include <filename>
Example
#include <stdio.h> #include "FORM.C"
File inclusion directive helps us to include files that contain declarations and function definitions. The angle brackets include standard library files, and double quotes are used for user-defined files. This feature allows code to be reused across different programs.
Compiler Control Directives
These are used to control the compiler actions. C pre-processor offers a feature called conditional compilation, which can be used to switch on or off based on a particular line or group of lines in a program.
For example, #ifdef and #ifndef can be used to include certain parts of the code only if a condition is true. This helps when dealing with platform-specific code or debugging parts of a program without affecting the entire code-base.
In more complex programs, conditional compilation gives control over the sections of code that are compiled and ensures that certain parts only run in specific environments or configurations.
Example
Let's say we have a code that runs differently on Windows and Linux:
#if def _WIN32 printf("This is Windows.
"); #else printf("This is Linux.
"); #endif
In this code, if the program is compiled on Windows, it will print "This is Windows." If compiled on Linux or any other system, it will print "This is Linux."