summaryrefslogtreecommitdiff
path: root/devdocs/c/preprocessor%2Fimpl.html
blob: 3ef1cf4aead18f65fc051112dd77038f7252cd17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    <h1 id="firstHeading" class="firstHeading">Implementation defined behavior control</h1>            <p>Implementation defined behavior is controlled by <code>#pragma</code> directive.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>#pragma</code> <span class="t-spar">pragma_params</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>_Pragma</code> <code>(</code> <span class="t-spar">string-literal</span> <code>)</code> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
</tr> 
</table> <div class="t-li1">
<span class="t-li">1)</span> Behaves in an implementation-defined manner (unless <span class="t-spar">pragma_params</span> is one of the standard pragmas shown below).</div> <div class="t-li1">
<span class="t-li">2)</span> Removes the encoding prefix (if any), the outer quotes, and leading/trailing whitespace from <span class="t-spar">string-literal</span>, replaces each <code>\"</code> with <code>"</code> and each <code>\\</code> with <code>\</code>, then tokenizes the result (as in <a href="../language/translation_phases" title="c/language/translation phases">translation stage 3</a>), and then uses the result as if the input to <code>#pragma</code> in <span class="t-v">(1)</span>.</div> <h3 id="Explanation"> Explanation</h3> <p>The pragma directive controls implementation-specific behavior of the compiler, such as disabling compiler warnings or changing alignment requirements. Any pragma that is not recognized is ignored.</p>
<h3 id="Standard_pragmas"> Standard pragmas</h3> <p>The following three pragmas are defined by the language standard:</p>
<table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>#pragma STDC FENV_ACCESS </code><span class="t-spar">arg</span> </td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma STDC FP_CONTRACT </code><span class="t-spar">arg</span> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma STDC CX_LIMITED_RANGE </code><span class="t-spar">arg</span> </td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
</tr> 
</table> <p>where <span class="t-spar">arg</span> is either <code>ON</code> or <code>OFF</code> or <code>DEFAULT</code>.</p>
<div class="t-li1">
<span class="t-li">1)</span> If set to <code>ON</code>, informs the compiler that the program will access or modify <a href="../numeric/fenv" title="c/numeric/fenv">floating-point environment</a>, which means that optimizations that could subvert flag tests and mode changes (e.g., global common subexpression elimination, code motion, and constant folding) are prohibited. The default value is implementation-defined, usually <code>OFF</code>.</div> <div class="t-li1">
<span class="t-li">2)</span> Allows <i>contracting</i> of floating-point expressions, that is optimizations that omit rounding errors and floating-point exceptions that would be observed if the expression was evaluated exactly as written. For example, allows the implementation of <code>(x * y) + z</code> with a single fused multiply-add CPU instruction. The default value is implementation-defined, usually <code>ON</code>.</div> <div class="t-li1">
<span class="t-li">3)</span> Informs the compiler that multiplication, division, and absolute value of complex numbers may use simplified mathematical formulas (x+iy)×(u+iv) = (xu-yv)+i(yu+xv), (x+iy)/(u+iv) = [(xu+yv)+i(yu-xv)]/(u<sup class="t-su">2</sup>+v<sup class="t-su">2</sup>), and |x+iy| = <span class="t-mrad"><span>√</span><span>x<sup class="t-su">2</sup>+y<sup class="t-su">2</sup></span></span>, despite the possibility of intermediate overflow. In other words, the programmer guarantees that the range of the values that will be passed to those function is limited. The default value is <code>OFF</code>
</div> <p>Note: compilers that do not support these pragmas may provide equivalent compile-time options, such as gcc's <code>-fcx-limited-range</code> and <code>-ffp-contract</code>.</p>
<h3 id="Non-standard_pragmas"> Non-standard pragmas</h3> <h4 id=".23pragma_once"> <span class="co2">#pragma once</span>
</h4> <p><code>#pragma once</code> is a non-standard pragma that is supported by the <a href="https://en.wikipedia.org/wiki/Pragma_once#Portability" class="extiw" title="enwiki:Pragma once">vast majority of modern compilers</a>. If it appears in a header file, it indicates that it is only to be parsed once, even if it is (directly or indirectly) included multiple times in the same source file.</p>
<p>Standard approach to preventing multiple inclusion of the same header is by using <a href="https://en.wikipedia.org/wiki/Include_guard" class="extiw" title="enwiki:Include guard">include guards</a>:</p>
<div class="c source-c"><pre data-language="c">#ifndef LIBRARY_FILENAME_H
#define LIBRARY_FILENAME_H
// contents of the header
#endif /* LIBRARY_FILENAME_H */</pre></div> <p>So that all but the first inclusion of the header in any translation unit are excluded from compilation. All modern compilers record the fact that a header file uses an include guard and do not re-parse the file if it is encountered again, as long as the guard is still defined (see e.g. <a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html">gcc</a>).</p>
<p>With <code>#pragma once</code>, the same header appears as</p>
<div class="c source-c"><pre data-language="c">#pragma once
// contents of the header</pre></div> <p>Unlike header guards, this pragma makes it impossible to erroneously use the same macro name in more than one file. On the other hand, since with <code>#pragma once</code> files are excluded based on their filesystem-level identity, this can't protect against including a header twice if it exists in more than one location in a project.</p>
<h4 id=".23pragma_pack"> <span class="co2">#pragma pack</span>
</h4> <p>This family of pragmas control the maximum alignment for subsequently defined structure and union members.</p>
<table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>#pragma pack(<span class="t-spar">arg</span>) </code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma pack() </code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma pack(push) </code> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma pack(push, <span class="t-spar">arg</span>) </code> </td> <td> (4) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>#pragma pack(pop) </code> </td> <td> (5) </td> <td class="t-sdsc-nopad"> </td>
</tr> 
</table> <p>where <span class="t-spar">arg</span> is a small power of two and specifies the new alignment in bytes.</p>
<div class="t-li1">
<span class="t-li">1)</span> Sets the current alignment to value <span class="t-spar">arg</span>.</div> <div class="t-li1">
<span class="t-li">2)</span> Sets the current alignment to the default value (specified by a command-line option).</div> <div class="t-li1">
<span class="t-li">3)</span> Pushes the value of the current alignment on an internal stack.</div> <div class="t-li1">
<span class="t-li">4)</span> Pushes the value of the current alignment on the internal stack and then sets the current alignment to value <span class="t-spar">arg</span>.</div> <div class="t-li1">
<span class="t-li">5)</span> Pops the top entry from the internal stack and then sets (restores) the current alignment to that value.</div> <p><code>#pragma pack</code> may decrease the alignment of a structure, however, it cannot make a structure overaligned.</p>
<p>See also specific details for <a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Structure-Layout-Pragmas.html">GCC</a> and <a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/cpp/preprocessor/pack">MSVC</a>.</p>
<h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 6.10.6 Pragma directive (p: 127) </li>
<li> 6.10.9 Pragma operator (p: 129) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 6.10.6 Pragma directive (p: 174) </li>
<li> 6.10.9 Pragma operator (p: 178) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 6.10.6 Pragma directive (p: 159) </li>
<li> 6.10.9 Pragma operator (p: 161-162) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.8.6 Pragma directive </li></ul>
</ul>                  <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/preprocessor/impl" title="cpp/preprocessor/impl">C++ documentation</a></span> for <span class=""><span>Implementation defined behavior control</span></span> </td>
</tr> </table> <h3 id="External_links"> External links</h3> <table> <tr style="vertical-align:top;"> <td>1. </td> <td>
<a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?view=vs-2019">C++ pragmas in Visual Studio 2019</a> </td>
</tr> <tr style="vertical-align:top;"> <td>2. </td> <td>
<a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Pragmas.html">Pragmas</a> accepted by GCC </td>
</tr> <tr style="vertical-align:top;"> <td>3. </td> <td>
<a rel="nofollow" class="external text" href="https://www.ibm.com/support/knowledgecenter/en/SSGH3R_16.1.0/com.ibm.xlcpp161.aix.doc/compiler_ref/pragma_descriptions.html">Individual pragma descriptions</a> and <a rel="nofollow" class="external text" href="https://www.ibm.com/support/knowledgecenter/en/SSGH3R_16.1.0/com.ibm.xlcpp161.aix.doc/language_ref/std_pragmas.html">Standard pragmas</a> in IBM AIX XL C 16.1 </td>
</tr> <tr style="vertical-align:top;"> <td>4. </td> <td>
<a rel="nofollow" class="external text" href="http://download.oracle.com/docs/cd/E19422-01/819-3690/Pragmas_App.html#73499">Appendix B. Pragmas</a> in Sun Studio 11 C++ User's Guide </td>
</tr> <tr style="vertical-align:top;"> <td>5. </td> <td>
<a rel="nofollow" class="external text" href="https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/pragmas.html">Intel C++ compiler pragmas</a> </td>
</tr> <tr style="vertical-align:top;"> <td>6. </td> <td>
<a rel="nofollow" class="external text" href="http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/Online_Help/pragmas.htm">HP aCC compiler pragmas</a> </td>
</tr>
</table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/preprocessor/impl" class="_attribution-link">https://en.cppreference.com/w/c/preprocessor/impl</a>
  </p>
</div>