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. 

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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

  1. A developer writes a C program-> and the program checks if there are any preprocessor directives available.
  2. 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.
  3. 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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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. 

Syntax:

#define name substitute text

Where,

  • 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

void main()

{

     int j;

     for (j=1;i<=PrintLOWER; j++)

     {

          printf("\n%d", j);

     }

}

Example: Macros With Certain Arguments

#define AREA(a) (5.18 * a * a)

void main()

{

     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.

Syntax:

#include TypeYourfilename

  • 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.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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.

Syntax:

#if TESTYourCondition <= 8

statement 1;

statement 2;

statement 3;

statement 4;

#else

statement 5;

statement 6;

statement 7;

#endif

4. Miscellaneous Directives

There are two miscellaneous directives apart from the above directives that are not commonly used.

  1. #undef: We can use this directive with the #define directive. It is used to undefine a specified macro.
  2. #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

#include <yourincludefile>

using namespace Emp;

void Empfun1();

void Empfun2();

#pragma startup Empfun1

#pragma exit Empfun2

void Empfun1()

{

cout << "Print the logic on Empfun1()\n";

}

void Empfun2()

{

cout << "Print the logic on Empfun2()\n";

}

int main()

{

void Empfun1();

void Empfun2();

cout << "Print main output ()\n";

return 0;

}

Output: 

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().

Example:

#include<stdio.h>

void testFun1(();

void testFun2();

#pragma startup testFun1

#pragma exit testFun2

void testFun1()

{

printf("It is Inside the testFun1()\n");

}

void testFun2()

{

printf("It is Inside the func2()\n");

}

int main()

{

printf("It is Inside the function of main()\n");

return 0;

}

Output

It is Inside the testFun1()

It is Inside the function of main()

It is Inside the func2()

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

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. 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.