473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The meaning of #line

Hi, everyone

I saw the preprocessor "#line " in some source codes.
It was like this :
#line 2972 "ifupdown.nw"

Please tell me what is the meaning of that "#line"?

Thanks in advance.

Regards,
J.H.Kim

Nov 21 '08 #1
7 2859
"J.H.Kim" <fr******@gmail.comwrites:
Hi, everyone

I saw the preprocessor "#line " in some source codes.
It was like this :
#line 2972 "ifupdown.nw"

Please tell me what is the meaning of that "#line"?
In simple terms, it tells the compiler that the following lines
come from (via some #include) the file 'ifupdown.nw', starting
at line 2972. That way if it needs to print a diagnostic message
to you, it can help point you in the direction of the cause of
the diagnostic message.

Famous places where this extra information can be, on the surface,
completely useless include things like:

"""
struct { int x; } y // no semicolon
#include <stdio.h // compiler will detect something amiss in here
"""
which produces:
"""
In file included from /usr/include/stdio.h:34,
from /tmp/crap.c:2:
/usr/lib/gcc/i486-linux-gnu/4.3.2/include/stddef.h:214: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'typedef'
[...]
"""

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
Nov 21 '08 #2
On Nov 20, 7:43*pm, "J.H.Kim" <frog1...@gmail.comwrote:
Hi, everyone

I saw the preprocessor "#line " in some source codes.
It was like this :
#line 2972 "ifupdown.nw"

Please tell me what is the meaning of that "#line"?
From the C Standard, we have this:

"6.10.4 Line control
Constraints
1 The string literal of a #line directive, if present, shall be a
character string literal.
Semantics
2 The line number of the current source line is one greater than the
number of new-line characters read or introduced in translation phase
1 (5.1.1.2) while processing the source file to the current token.
3 A preprocessing directive of the form
# line digit-sequence new-line
causes the implementation to behave as if the following sequence of
source lines begins with a source line that has a line number as
specified by the digit sequence (interpreted as a decimal integer).
The digit sequence shall not specify zero, nor a number greater than
2147483647.
4 A preprocessing directive of the form
# line digit-sequence "s-char-sequenceopt" new-line
sets the presumed line number similarly and changes the presumed name
of the source file to be the contents of the character string literal.
5 A preprocessing directive of the form
# line pp-tokens new-line
(that does not match one of the two previous forms) is permitted. The
preprocessing tokens after line on the directive are processed just as
in normal text (each identifier currently defined as a macro name is
replaced by its replacement list of preprocessing tokens). The
directive resulting after all replacements shall match one of the two
previous forms and is then processed as appropriate.
158 Language §6.10.4"
Nov 21 '08 #3
J.H.Kim wrote:
Hi, everyone

I saw the preprocessor "#line " in some source codes.
It was like this :
#line 2972 "ifupdown.nw"

Please tell me what is the meaning of that "#line"?
It means that from this point onward in the file, __FILE__ should be
"ifupdown.nw". It also means that __LINE__ should be re-set to 2972, and
start incrementing from there with each newline.

Traditionally, translation phases 1-4 were performed by a separate
pre-processor program, usually called cpp. It is still commonplace for
compilers to provide an option whereby only translation phases 1-4 are
performed; all of the compilers I use most frequently do this when given
a -E command line option. In either case, you're likely to find lots of
#line directives in the output. They are used by the C compiler to
generate error messages that refer to the correct line number in the
correct file name, even when that file was #included.

However, you're also free to insert #line directives of your own, though
I can't imagine any good reason for doing so. I can think of some bad
reasons: imagine the confusion you could create with a #line directive
that sets __FILE__ to "stdio.h".
Nov 21 '08 #4
James Kuyper wrote:
[...]
However, you're also free to insert #line directives of your own, though
I can't imagine any good reason for doing so. I can think of some bad
reasons: imagine the confusion you could create with a #line directive
that sets __FILE__ to "stdio.h".
One frequent use for #line in "open code" is when the
C source is generated mechanically from some über-source in
another language. The über-to-C translator might insert
#line directives in the generated C to make the compiler's
error messages refer to the über-source (which the programmer
presumably thinks of as "the" source) rather than to the C
source (which the programmer might not even see in the
ordinary course of business).

I believe some yacc versions do this, so the compiler's
complaints are about the .y file rather than about the
generated .c file.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '08 #5

"Eric Sosman" <es*****@ieee-dot-org.invalidha scritto nel messaggio
news:gg**********@news.motzarella.org...
I believe some yacc versions do this, so the compiler's
complaints are about the .y file rather than about the
generated .c file.
In fact you do have lots of "#line" in Bison's output for a .y grammar. The
problm with that is if there's an error, the line number is referring to is
always wrong, because the error was in the grammar and not in the generated
c source, so I don't know how useful is that...
Nov 21 '08 #6
Lorenzo Villari wrote:
"Eric Sosman" <es*****@ieee-dot-org.invalidha scritto nel messaggio
news:gg**********@news.motzarella.org...
> I believe some yacc versions do this, so the compiler's
complaints are about the .y file rather than about the
generated .c file.

In fact you do have lots of "#line" in Bison's output for a .y grammar. The
problm with that is if there's an error, the line number is referring to is
always wrong, because the error was in the grammar and not in the generated
c source, so I don't know how useful is that...
Haven't used bison, but the yacc-ity yaccs I've known have all
allowed the programmer to embed chunks of almost-verbatim C code in
the .y file along with the grammar productions. If the eventual C
compiler spots something wrong with the embedded C code, having the
complaints refer to a location in the .y file is quite helpful.

--
Er*********@sun.com
Nov 21 '08 #7
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
James Kuyper wrote:
>[...]
However, you're also free to insert #line directives of your own,
though I can't imagine any good reason for doing so.
One frequent use for #line in "open code" is when the
C source is generated mechanically from some über-source in
another language. The über-to-C translator might insert
#line directives in the generated C to make the compiler's
error messages refer to the über-source (which the programmer
presumably thinks of as "the" source) rather than to the C
source (which the programmer might not even see in the
ordinary course of business).
As in the OP's example (reinserted and rearranged here)
>J.H.Kim wrote:
>>#line 2972 "ifupdown.nw"
where the other language is most likely Noweb, a Literate Programming
tool.

mlp
Nov 21 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const...
7
by: John Baker | last post by:
HI: I see the term "Me!" in a lot of Access statements, bit don't quite know what it means. In the same contexts as wondering what the meaning if "is" is, I wonder what the meaning of "Me!" is....
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
9
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such...
2
by: Chameleon | last post by:
the code below... ------------------------------- <? function errorHandler($errno, $str, $file, $line) { //if ($errno == E_STRICT) return; fwrite(STDERR, "\n\n$errno: $str\n\n"); if ($errno ==...
1
by: mar-kav | last post by:
Hello, i'm a beginner so exuse me: i installed IIS 5.1, Framework1.1, vs .NET 2003 i put my web dir' in inetpub/wwwroot, tried to broows to my homepage under localhost & got this error message: ...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
3
by: sam | last post by:
HI, The code is as follows:- #include <stdio.h> 1 2 void * work(void * ptr) 3 { 4 int i; 5 for (i = 0; i < 10; i++) 6 {
9
by: metdos | last post by:
What is the meaning of this code line? x += b <? mid; Especially <? Thanks
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.