We can consider a preprocessor as a compilation process, which runs when the developer runs the program. It is a pre-process of execution of a program using c/c++ language. To initialize a process of preprocessor commands, it's mandated to define with a hash symbol (#). It can preferably be the non-blank character, and for better readability, a preprocessor directive should start in the first column.
List of Preprocessor Directives
To execute a preprocessor program on a certain statement, some of the preprocessor directives types are:
- #define: It substitutes a preprocessor using macro.
- #include: It helps to insert a certain header from another file.
- #undef: It undefines a certain preprocessor macro.
- #ifdef: It returns true if a certain macro is defined.
- #ifndef: It returns true if a certain macro is not defined.
- #if, #elif, #else, and #endif: It tests the program using a certain condition; these directives can be nested too.
- #line: It handles the line numbers on the errors and warnings. It can be used to change the line number and source files while generating output during compile time.
- #error and #warning: It can be used for generating errors and warnings.
- #error can be performed to stop compilation.
- #warning is performed to continue compilation with messages in the console window.
- #region and #endregion: To define the sections of the code to make them more understandable and readable, we can use the region using expansion and collapse features.
Process Flow of Preprocessor Directives
- A developer writes a C program-> and the program checks if there are any preprocessor directives available.
- If available, it will perform the action event of pre-processor and the compiler will generate the object code. The code will then be executed by the linker.
- In case no preprocessor directives are available, it will go to the compiler. The compiler will then generate the object code followed by execution of the code by linker.
Here in the article, we will look at the various examples of preprocessor directives.
Four Major Types of Preprocessor Directives
1. Macro Expansion
In Macro expansion, we can specify two types of Macros with arguments:
We can also pass arguments to macros; it can be described with arguments that perform similarly as functions.
#define name substitute text
- name: Here, we can define the micro template.
- replacement text : we can define it as the macro expansion.
- To write a macro name, we need to use capital letters.
- For better readability, we can define suitable names on certain macros.
- To modify program: We can change only the macro and it can reflect on the program. Hence we do not need to change it every time.
Example: Basic Macro
#define PrintLOWER 50
for (j=1;i<=PrintLOWER; j++)
Example: Macros With Certain Arguments
#define AREA(a) (5.18 * a * a)
float r = 3.5, x;
x = AREA (r);
printf ("\n Area of circle = %f", x);
Types of Predefined Macros
- ___TIME___ defines the current time using a character literal in “HH:MM: SS” format.
- ___STDC___ specifies as 1 when the compiler complies with the ANSI standard.
- ___TIMESTAMP___ specifies the timestamp “DDD MM YYYY Date HH:MM: SS”. It is used to define the date and time of the latest modification of the present source file.
- ___LINE___ consists of a present line number with a decimal constant.
- ___FILE___ includes the current filename using a string literal.
- ___DATE___ shows the present date with a character literal in the “MMM DD YYYY” format.
2. File Inclusion
For file inclusion, we can use the #include.
- We can replace the content that is comprised in the filename at the point where a certain directive is written.
- We can use a file inclusive directive and include the header files in the programs.
- We can integrate function declaration, macros, and declarations of the external variables in the top header file rather than repeating each logic in the program.
- The stdio.h header file includes the function declarations and can provide the details about the input and output.
Example of the file inclusion statement:
- i) #include “DefineYourfile-name”: In this example, we can search a file within the current working directory through easy search.
- ii) #include <DefineYourfile-name>: In this example, we can define a certain directory, and we search a file within it.
3. Conditional Compilation
- We can use conditional compilation on a certain logic where we need to define a condition logics.
- It utilizes directives like #if, #elif, #else, and #endif.
#if TESTYourCondition <= 8
4. Miscellaneous Directives
There are two miscellaneous directives apart from the above directives that are not commonly used.
- #undef: We can use this directive with the #define directive. It is used to undefine a specified macro.
- #pragma: We can use this directive on a certain level where we need to define turn-on or off specific features. We can use these types of directives for the compiler-specific, which has a certain range as complier to the compiler. Example of #pragma directives are discussed below:
- #pragma startup and #pragma exit: These directives push to indicate the functions that are required to run before the program as a startup (before the control moves to main()) and before program exit (only before the control returns from main()).
Code Example: #pragma Directives
using namespace Emp;
#pragma startup Empfun1
#pragma exit Empfun2
cout << "Print the logic on Empfun1()\n";
cout << "Print the logic on Empfun2()\n";
cout << "Print main output ()\n";
Print the logic on Empfun1()
Print main output ()
Print the logic on Empfun2()
#pragma warn Directive:
Pragma directive is the certain directive that we can utilize to make it turn off and on to enable certain features. Pragma specifies the range from one compiler to another. Microsoft C compiler provides the pragmas that provide the listing and placing ways to give the comments in the object file that is generated by the compiler. Prgama has its custom-specific implementation rules directives that can be defined as per the certain scenario.
pragma startup and #pragma exit: These pragma directives are defined as the certain functions that can be used to determine to run before program startup that can be specified before to main() and before program exit can be applied before the control returns from main().
#pragma startup testFun1
#pragma exit testFun2
printf("It is Inside the testFun1()\n");
printf("It is Inside the func2()\n");
printf("It is Inside the function of main()\n");
It is Inside the testFun1()
It is Inside the function of main()
It is Inside the func2()
We hope this article helped you understand the Preprocessor Directives. In this article, we discussed the various definitions and techniques of the Preprocessor Directives with the sample code using c# or C++/C programming. We also learned their types with respective syntax and example. This article will be helpful to professional developers from application development from the .net and JAVA backgrounds, application architectures, cloud specialists, testers, and other learners looking for different uses of various types of mechanisms of Spring Data.
Besides pursuing varied courses provided by Simplilearn, you can also sign up on our SkillUp platform, a Simplilearn initiative, which offers multiple free online courses to help learners understand the basics of numerous programming languages, including Preprocessor Directives in C or C++. You can also opt for our Full-stack Web Development Certification Course to improve your career prospects by mastering both backend and frontend with other tools like SpringBoot, AngularMVC, JSPs, and Hibernate.