How does pragma work




















Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Yes No. Any additional feedback? Skip Submit. If any of those identifiers appears anywhere in the source after the directive, it is a hard error. If a poisoned identifier appears as part of the expansion of a macro which was defined before the identifier was poisoned, it will not cause an error.

This lets you poison an identifier without worrying about system headers defining macros that use it. This pragma takes no arguments. It causes the rest of the code in the current file to be treated as if it came from a system header. See System Headers. The message contained in the pragma must be a single string literal.

Here we have a standard way of implementing a pragma directive within a program, which begins with a hashtag followed by the pragma keyword and then a simple token string.

Syntax 2 is similar to the first one with the only difference that we use a different operator before the keyword pragma:. With our second format of pragma syntax, we have two underscores prior to the pragma keyword. Few of the most commonly used token strings are a startup, exit, warn, etc. We will learn about these tokens with the help of examples and see the demonstration. We will now move on to understanding how the pragma directive in C actually works.

We have already learned that the pragma directive is used to notify the compiler of any special event that is to happen. That is exactly how the pragma directive works within C, these pragma directives are executed before compilation and followed by preprocessing. This way, the pragma directive is able to notify the compiler to what to ignore and what not to ignore.

It is also recommended to use the pragma directive once over in a program. Now that we have understood how the pragma works in C, let us move on to demonstrating some examples. Our first example is a simple output statement. We will simply implement the pragma directive with two of its tokens which are startup and exit. The code for the program is as follows:. Code Explanation: Started with a simple system include files that are required for the program. Then we have a single function, followed by the pragma definitions for both startup and exit.

Then we have another function that holds the print statement. Followed by the getch function, which stands for getch and is used to hold the output screen after the output is printed and wait until the user provides an input to exit.

Finally, we have our main code which holds a simple print statement and a getch function. Code Explanation: Started with single include file then a few pragma derivatives.



0コメント

  • 1000 / 1000