We will imagine a preprocessor as a compilation task, which runs when the developer runs this system. This is a pre-process of execution of a program the use of c/c++ language. To initialize a technique of preprocessor instructions, it is mandated to outline with a hash image (#). It might probably ideally be the non-blank persona, and for higher clarity, a preprocessor directive will have to get started within the first column.
Record of Preprocessor Directives
To execute a preprocessor program on a definite observation, one of the vital preprocessor directives varieties are:
- #outline: It substitutes a preprocessor the use of macro.
- #come with: It is helping to insert a definite header from any other document.
- #undef: It undefines a definite preprocessor macro.
- #ifdef: It returns true if a definite macro is outlined.
- #ifndef: It returns true if a definite macro isn’t outlined.
- #if, #elif, #else, and #endif: It exams this system the use of a definite situation; those directives will also be nested too.
- #line: It handles the road numbers at the mistakes and warnings. It may be used to switch the road quantity and supply information whilst producing output all the way through bring together time.
- #error and #caution: It may be used for producing mistakes and warnings.
- #error will also be carried out to forestall compilation.
- #caution is carried out to proceed compilation with messages within the console window.
- #area and #endregion: To outline the sections of the code to cause them to extra comprehensible and readable, we will use the area the use of growth and cave in options.
Procedure Go with the flow of Preprocessor Directives
- A developer writes a C program-> and this system tests if there are any preprocessor directives to be had.
- If to be had, it’ll carry out the motion match of pre-processor and the compiler will generate the article code. The code will then be completed by way of the linker.
- In case no preprocessor directives are to be had, it’ll pass to the compiler. The compiler will then generate the article code adopted by way of execution of the code by way of linker.
Right here within the article, we will be able to have a look at the quite a lot of examples of preprocessor directives.
4 Primary Sorts of Preprocessor Directives
1. Macro Enlargement
In Macro growth, we will specify two forms of Macros with arguments:
We will additionally cross arguments to macros; it may be described with arguments that carry out in a similar fashion as purposes.
Syntax:
#outline call exchange textual content
The place,
- call: Right here, we will outline the micro template.
- alternative textual content : we will outline it because the macro growth.
- To put in writing a macro call, we wish to use capital letters.
- For higher clarity, we will outline appropriate names on sure macros.
- To switch program: We will trade simplest the macro and it might replicate at the program. Therefore we don’t wish to trade it each and every time.
Instance: Fundamental Macro
#outline PrintLOWER 50
void primary()
{
int j;
for (j=1;i<=PrintLOWER; j++)
{
printf(“npercentd”, j);
}
}
Instance: Macros With Positive Arguments
#outline AREA(a) (5.18 * a * a)
void primary()
{
flow r = 3.5, x;
x = AREA (r);
printf (“n Space of circle = %f”, x);
}
Sorts of Predefined Macros
- ___TIME___ defines the present time the use of a personality literal in “HH:MM: SS” structure.
- ___STDC___ specifies as 1 when the compiler complies with the ANSI usual.
- ___TIMESTAMP___ specifies the timestamp “DDD MM YYYY Date HH:MM: SS”. It’s used to outline the date and time of the newest amendment of the current supply document.
- ___LINE___ is composed of a gift line quantity with a decimal consistent.
- ___FILE___ comprises the present filename the use of a string literal.
- ___DATE___ displays the prevailing date with a personality literal within the “MMM DD YYYY” structure.
2. Document Inclusion
For document inclusion, we will use the #come with.
Syntax:
#come with TypeYourfilename
- We will exchange the content material this is comprised within the filename on the level the place a definite directive is written.
- We will use a document inclusive directive and come with the header information within the methods.
- We will combine serve as declaration, macros, and declarations of the exterior variables within the best header document moderately than repeating every common sense in this system.
- The stdio.h header document comprises the serve as declarations and will give you the information about the enter and output.
Instance of the document inclusion observation:
- i) #come with “DefineYourfile-name”: On this instance, we will seek a document inside the present running listing via simple seek.
- ii) #come with <DefineYourfile-name>: On this instance, we will outline a definite listing, and we seek a document inside of it.
3. Conditional Compilation
- We will use conditional compilation on a definite common sense the place we wish to outline a situation logics.
- It makes use of directives like #if, #elif, #else, and #endif.
Syntax:
#if TESTYourCondition <= 8
observation 1;
observation 2;
observation 3;
observation 4;
#else
observation 5;
observation 6;
observation 7;
#endif
4. Miscellaneous Directives
There are two miscellaneous directives except the above directives that aren’t usually used.
- #undef: We will use this directive with the #outline directive. It’s used to undefine a specified macro.
- #pragma: We will use this directive on a definite stage the place we wish to outline turn-on or off particular options. We will use some of these directives for the compiler-specific, which has a definite vary as complier to the compiler. Instance of #pragma directives are mentioned beneath:
- #pragma startup and #pragma go out: Those directives push to suggest the purposes which can be required to run earlier than this system as a startup (earlier than the keep watch over strikes to primary()) and earlier than program go out (simplest earlier than the keep watch over returns from primary()).
Code Instance: #pragma Directives
#come with <yourincludefile>
the use of namespace Emp;
void Empfun1();
void Empfun2();
#pragma startup Empfun1
#pragma go out Empfun2
void Empfun1()
{
cout << “Print the common sense on Empfun1()n”;
}
void Empfun2()
{
cout << “Print the common sense on Empfun2()n”;
}
int primary()
{
void Empfun1();
void Empfun2();
cout << “Print primary output ()n”;
go back 0;
}
Output:
Print the common sense on Empfun1()
Print primary output ()
Print the common sense on Empfun2()
#pragma warn Directive:
Pragma directive is the sure directive that we will make the most of to make it flip on and off to permit sure options. Pragma specifies the variety from one compiler to any other. Microsoft C compiler supplies the pragmas that give you the list and hanging tactics to present the feedback within the object document this is generated by way of the compiler. Prgama has its custom-specific implementation laws directives that may be outlined as consistent with the sure situation.
pragma startup and #pragma go out: Those pragma directives are outlined because the sure purposes that can be utilized to resolve to run earlier than program startup that may be specified earlier than to primary() and earlier than program go out will also be implemented earlier than the keep watch over returns from primary().
Instance:
#come with<stdio.h>
void testFun1(();
void testFun2();
#pragma startup testFun1
#pragma go out testFun2
void testFun1()
{
printf(“It’s Throughout the testFun1()n”);
}
void testFun2()
{
printf(“It’s Throughout the func2()n”);
}
int primary()
{
printf(“It’s Throughout the serve as of primary()n”);
go back 0;
}
Output
It’s Throughout the testFun1()
It’s Throughout the serve as of primary()
It’s Throughout the func2()
Conclusion
We are hoping this newsletter helped you recognize the Preprocessor Directives. On this article, we mentioned the quite a lot of definitions and methods of the Preprocessor Directives with the pattern code the use of c# or C++/C programming. We additionally realized their varieties with respective syntax and instance. This article is going to be useful to skilled builders from software construction from the .internet and JAVA backgrounds, software architectures, cloud consultants, testers, and different newbies searching for other makes use of of quite a lot of forms of mechanisms of Spring Knowledge.
But even so pursuing numerous lessons equipped by way of Simplilearn, you’ll additionally enroll on our SkillUp platform, a Simplilearn initiative, which provides a couple of loose on-line lessons to assist newbies perceive the fundamentals of a lot of programming languages, together with Preprocessor Directives in C or C++. You’ll be able to additionally go for our Complete Stack Developer – MERN Stack to enhance your profession potentialities by way of mastering each backend and frontend with different equipment like SpringBoot, AngularMVC, JSPs, and Hibernate.
supply: www.simplilearn.com