instagram youtube
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
logo
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Preprocessor Directives in C | Simplilearn

- Team

Jumat, 19 Juli 2024 - 22:50

facebook twitter whatsapp telegram line copy

URL berhasil dicopy

facebook icon twitter icon whatsapp icon telegram icon line icon copy

URL berhasil dicopy


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. 

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

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

  1. A developer writes a C program-> and this system tests if there are any preprocessor directives to be had.
  2. 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.
  3. 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.

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

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.

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

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

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

4. Miscellaneous Directives

There are two miscellaneous directives except the above directives that aren’t usually used.

  1. #undef: We will use this directive with the #outline directive. It’s used to undefine a specified macro.
  2. #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()

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

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

Need a Best Tool Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

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

Berita Terkait

Most sensible Recommended Engineering Tactics | 2025
Unfastened Flow Vs General Flow
Be told How AI Automation Is Evolving in 2025
What Is a PHP Compiler & The best way to use it?
Best Leadership Books You Should Read in 2024
Best JavaScript Examples You Must Try in 2025
How to Choose the Right Free Course for the Best Value of Time Spent
What Is Product Design? Definition & Key Principles
Berita ini 7 kali dibaca

Berita Terkait

Selasa, 11 Februari 2025 - 22:32

Revo Uninstaller Pro 5.3.5

Selasa, 11 Februari 2025 - 22:21

Rhinoceros 8.15.25019.13001

Selasa, 11 Februari 2025 - 22:12

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Februari 2025 - 22:08

RoboDK 5.9.0.25039

Selasa, 11 Februari 2025 - 22:05

RoboTask 10.2.2

Selasa, 11 Februari 2025 - 21:18

Room Arranger 10.0.1.714 / 9.6.2.625

Selasa, 11 Februari 2025 - 17:14

Team11 v1.0.2 – Fantasy Cricket App

Selasa, 11 Februari 2025 - 16:20

Sandboxie 1.15.6 / Classic 5.70.6

Berita Terbaru

Headline

Revo Uninstaller Pro 5.3.5

Selasa, 11 Feb 2025 - 22:32

Headline

Rhinoceros 8.15.25019.13001

Selasa, 11 Feb 2025 - 22:21

Headline

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Feb 2025 - 22:12

Headline

RoboDK 5.9.0.25039

Selasa, 11 Feb 2025 - 22:08

Headline

RoboTask 10.2.2

Selasa, 11 Feb 2025 - 22:05