# Pragma and _ Pragma

Source: Internet
Author: User
Tags processing instruction
C90 brings a new member to the pre-processing instruction family: # pragma. Generally, we seldom see it.

# The role of Pragma is to provide specific compilation instructions for a specific compiler. These instructions are specific to one (or some) compiler, other compilers may not know the meaning of the directive or have different understandings of the directive, that is, # The implementation of Pragma is related to a specific platform. To help you understand # pragma usage, hp c compiler is used as an example. The hp c compiler runs primarily on the HP-UX platform and is generally used in the same way as GCC, for example, to compile a program: $ CC example. c. If we explicitly instruct the compiler to optimize the code, compile it like this: $ CC + on example. in C, N can be 1, 2, 3, and 4, representing different levels of optimization, for example, $ CC + O2 example. c This command instructs the hp c compiler to perform the entire example. C Code adopts 2nd-level optimized compilation. However, in some cases, we may need to treat a part of the code specially. For example, to suspend optimization, the hp c compiler provides several ways to implement it. One of them is to use # pragma: // Prog. c void F (){...} # pragma optimize off int g (){...} # pragma optimize on double H (){...} $ CC + O2 Prog. c above Prog. there are three functions in C. We want to use 2nd-level optimization to compile the entire code file. Except function g (), we decided not to optimize it for some reason. We can see that two # pragma commands can be used to achieve the goal. The first # pragma directive instructs the compiler to stop code optimization, so g () code is not optimized. Article 2 # The Pragma command notifies the compiler to re-start code optimization compilation (the optimization level is still Level 2 given by the previous command line), so from H () the initial code is optimized. Here, we take code optimization compilation as an example to briefly introduce # pragma usage. Readers must remember that the specific # pragma command may have different effects under different circumstances. Assuming that two vendors have released their own C compilers, they may use the same # pragma command at the same time, but their implementations are different, in this way, the compiled code may have unexpected results. Therefore, to ensure that the # pragma command can be correctly interpreted, we usually need to use other pre-processing commands for cooperation, such :... # ifdef _ HPUX # pragma float_traps_on _ all # endif... in the above example, only the hp c compiler that defines the "_ HPUX" macro will see the # pragma command. Other compilers, such as GCC, will not see it at all, because GCC does not define the "_ HPUX" macro, the content of the # pragma command is deleted as early as the preprocessing phase. Someone may ask: if the compiler sees what it doesn't know # Will The Pragma Command report an error? The answer is: no. Specifically, the meaning of a # pragma instruction is not the jurisdiction of the C standard. The compiler cannot refer to a program error because it does not know the # pragma instruction. The only practice is to ignore it. Example:/* example C code */# pragma unknown_directive unknown_option int main (void) {return 0 ;}$ GCC test. C $. /. out $ Although GCC does not know the # pragma command in the above Code, it compiles test. C is completely correct. Now, c99 provides a new keyword "_ Pragma" to complete similar functions. For example, # pragma optimize off can be written as _ Pragma ("optimize off") in c99. // note: the "_ Pragma" without semicolons is more reasonable than "# pragma" (in design) after the statement, so the function is also enhanced. For example, our compiler supports four different levels of optimization. If # pragma is used, write as follows: # pragma opt_level N // 1 ≤ n ≤ 4 Do You Think You Should repeat "# pragma... "very troublesome? If you can use a macro definition to simplify writing: # define opt_l (x) # pragma opt_level X, we only need to write: opt_l (3), which is equivalent to writing: # pragma opt_level 3. Unfortunately, in C90, this is always a dream! Because the character "#" has a special purpose in Preprocessing commands, it must be followed by a macro parameter name, for example: # define macro (x) # X, macro (example) as you can imagine, # define is used to define a macro about # pragma, which is not feasible. However, the new keyword "_ Pragma" is a good solution. Because _ Pragma does not have the character "#", we can safely define the macro: # define opt_l (X) pragma (opt_level X) # define Pragma (x) _ Pragma (# X) at this time, we only need to write: opt_l (2) After preprocessing, it becomes: _ Pragma ("opt_level 2"): # pragma opt_level 2

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.