blob: 92993911edff1e9ae91942ebe533a34b568afe58 (
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
|
<h1 id="firstHeading" class="firstHeading">Filename and line information</h1> <p>Changes the current line number and file name in the preprocessor.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>#line</code> <span class="t-spar">lineno</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr> <tr class="t-sdsc"> <td> <code>#line</code> <span class="t-spar">lineno</span> <code>"</code><span class="t-spar">filename</span><code>"</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <div class="t-li1">
<span class="t-li">1)</span> Changes the current preprocessor line number to <span class="t-spar">lineno</span>. Occurrences of the macro <code>__LINE__</code> beyond this point will expand to <span class="t-spar">lineno</span> plus the number of actual source code lines encountered since.</div> <div class="t-li1">
<span class="t-li">2)</span> Also changes the current preprocessor file name to <span class="t-spar">filename</span>. Occurrences of the macro <code>__FILE__</code> beyond this point will produce <span class="t-spar">filename</span>.</div> <p>Any preprocessing tokens (macro constants or expressions) are permitted as arguments to <code>#line</code> as long as they expand to a valid decimal integer optionally following a valid character string.</p>
<p><span class="t-spar">lineno</span> must be a sequence of at least one decimal digit (the program is ill-formed, otherwise) and is always interpreted as decimal (even if it starts with <code>0</code>).</p>
<p>If <span class="t-spar">lineno</span> is <code>0</code> or greater than <span class="t-rev-inl t-until-c99"><span><code>32767</code></span><span><span class="t-mark-rev t-until-c99">(until C99)</span></span></span><span class="t-rev-inl t-since-c99"><span><code>2147483647</code></span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>, the behavior is undefined.</p>
<h3 id="Notes"> Notes</h3> <p>This directive is used by some automatic code generation tools which produce C source files from a file written in another language. In that case, <code>#line</code> directives may be inserted in the generated C file referencing line numbers and the file name of the original (human-editable) source file.</p>
<p>The line number following the directive <code>#line __LINE__</code> is unspecified (there are two possible values that <code>__LINE__</code> can expand to in this case: number of endlines seen so far, or number of endlines seen so far plus the endline that ends the <code>#line</code> directive). This is the result of <a rel="nofollow" class="external text" href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2257.htm#dr_464">DR 464</a>, which applies retroactively.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <assert.h>
#define FNAME "test.c"
int main(void)
{
#line 777 FNAME
assert(2+2 == 5);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">test: test.c:777: int main(): Assertion `2+2 == 5' failed.</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 6.10.4 Line control (p: 126) </li>
<li> J.1 Unspecified behavior </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.10.4 Line control (p: 173) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.10.4 Line control (p: 158) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.8.4 Line control </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/line" title="cpp/preprocessor/line">C++ documentation</a></span> for <span class=""><span>Filename and line information</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/preprocessor/line" class="_attribution-link">https://en.cppreference.com/w/c/preprocessor/line</a>
</p>
</div>
|