strings in #if 0

T

tedu

having read the section on preprocessing (6.10) and not finding the
answer, is the following code acceptable? i have compilers which
accept it, accept it with a warning, and refuse to compile it. i'm
wondering which is correct.

#if 0
print FILEPTR "int main()

#endif
 
B

Ben Pfaff

tedu said:
having read the section on preprocessing (6.10) and not finding the
answer, is the following code acceptable? i have compilers which
accept it, accept it with a warning, and refuse to compile it. i'm
wondering which is correct.

#if 0
print FILEPTR "int main()

#endif

It is incorrect. Groups that are skipped must consist of valid
preprocessing tokens.
 
N

Neil

Ben said:
It is incorrect. Groups that are skipped must consist of valid
preprocessing tokens.

No, it is fine. The group consists of valid preprocessing tokens, one
of which is ". These preprocessing tokens must be ignored as the
standard dictates.

A compiler must accept it.
 
K

Keith Thompson

Neil said:
No, it is fine. The group consists of valid preprocessing tokens, one
of which is ". These preprocessing tokens must be ignored as the
standard dictates.

A compiler must accept it.

No, it's undefined behavior. (We've had a claim that it's illegal,
and one that it's legal; I figured we should cover all the
possibilities.)

C99 6.4p3:

A _token_ is the minimal lexical element of the language in
translation phases 7 and 8. The categories of tokens are:
keywords, identifiers, constants, string literals, and
punctuators. A preprocessing token is the minimal lexical element
of the language in translation phases 3 through 6. The categories
of preprocessing tokens are: header names, identifiers,
preprocessing numbers, character constants, string literals,
punctuators, and single non-white-space characters that do not
lexically match the other preprocessing token categories. If a '
or a " character matches the last category, the behavior is
undefined.
 
R

Richard Bos

Neil said:
No, it is fine. The group consists of valid preprocessing tokens, one
of which is ". These preprocessing tokens must be ignored as the
standard dictates.

A compiler must accept it.

Wrong, and wrong. From 6.4#3:

# The categories of preprocessing tokens are: ... string literals ... ,
# and single non-whitespace characters that do not lexically match the
# other preprocessing token categories. If a ' or " character matches
# the last category, the behavior is undefined.

Note the last sentence.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top