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. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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 Developer - MERN Stack to improve your career prospects by mastering both backend and frontend with other tools like SpringBoot, AngularMVC, JSPs, and Hibernate. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 18 Dec, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449