why is this allowed if FOO is undefined: #if 1 < FOO

G

Guest

Hello,
K&R say about the constant expression after #if:
"The resulting constant expression is restricted: it must be integral
(...)"

Clearly, the expression
1 < FOO
if FOO is undefined, is not integral. Then why preprocessors do not
complain about the line

#if 1 < FOO

I tried both Gnu and MS, both don't complain, but seem to assume that
FOO is 0.

Thank you
Mark Galecki
 
P

Peter Nilsson

Hello,
K&R say about the constant expression after #if:
"The resulting constant expression is restricted: it must be integral
(...)"

Clearly, the expression
1 < FOO
if FOO is undefined, is not integral.

If FOO is not defined, then 0 is substituted. This only applies
to conditional preprocessing directives. Thus the following are
equivalent if FOO is not defined...

#ifdef FOO
#if FOO
 
A

Artie Gold

Hello,
K&R say about the constant expression after #if:
"The resulting constant expression is restricted: it must be integral
(...)"

Clearly, the expression
1 < FOO
if FOO is undefined, is not integral. Then why preprocessors do not
complain about the line

#if 1 < FOO

I tried both Gnu and MS, both don't complain, but seem to assume that
FOO is 0.

Thank you
Mark Galecki
That is correct behavior, hence the difference between:

#if FOO

and

#if defined(FOO)

HTH,
--ag
 
G

Guest

Thank you Peter and Artie for replying. Where does it say that 0 is
substituted? Can you show me in a standard or reference book?
 
C

Chris Torek

Thank you Peter and Artie for replying. Where does it say that 0 is
substituted? Can you show me in a standard or reference book?

Here is the text from a C99 draft, slightly edited for newsgroup
posting-ness. Read paragraph three carefully.

6.8.1 Conditional inclusion

Constraints

[#1] The expression that controls conditional inclusion
shall be an integer constant expression except that: it
shall not contain a cast; identifiers (including those
lexically identical to keywords) are interpreted as
described below;123 and it may contain unary operator
expressions of the form

defined identifier
or
defined ( identifier )

which evaluate to 1 if the identifier is currently defined
as a macro name (that is, if it is predefined or if it has
been the subject of a #define preprocessing directive
without an intervening #undef directive with the same
subject identifier), 0 if it is not.
__________
123. Because the controlling constant expression is
evaluated during translation phase 4, all identifiers
either are or are not macro names - there simply are no
keywords, enumeration constants, etc.
__________


Semantics

[#2] Preprocessing directives of the forms

# if constant-expr new-line group-opt
# elif constant-expr new-line group-opt

check whether the controlling constant expression evaluates
to nonzero.

[#3] Prior to evaluation, macro invocations in the list of
preprocessing tokens that will become the controlling
constant expression are replaced (except for those macro
names modified by the defined unary operator), just as in
normal text. If the token defined is generated as a result
of this replacement process or use of the defined unary
operator does not match one of the two specified forms prior
to macro replacement, the behavior is undefined. After all
replacements due to macro expansion and the defined unary
operator have been performed, all remaining identifiers are
replaced with the pp-number 0, and then each preprocessing
token is converted into a token. The resulting tokens
compose the controlling constant expression which is
evaluated according to the rules of 6.4, except that all
signed integer types and all unsigned integer types act as
if they have the same representation as, respectively, the
types intmax_t and uintmax_t defined in the header
<inttypes.h>. This includes interpreting character
constants, which may involve converting escape sequences
into execution character set members. Whether the numeric
value for these character constants matches the value
obtained when an identical character constant occurs in an
expression (other than within a #if or #elif directive) is
implementation-defined.124 Also, whether a single-character
character constant may have a negative value is
implementation-defined.
 
K

Kiru Sengal

Thank you Peter and Artie for replying. Where does it say that 0 is
substituted? Can you show me in a standard or reference book?

You should provide a context from previous posts that is relevant to
your post. People with newreaders can't tell what you mean by "where
does it say that 0 is substituted?"

To answer your question/request for "proof" of 0-substitution in
preprocessor conditional inclusions of undefined pp-identifiers, here
is something from the C99 Draft (N869)....

6.10.1 Conditional Inclusion

Semantics #3

Prior to evaluation, macro invocations in the list of preprocessing
tokens that will become the controlling constant expression are
replaced (except for those macro names modified by the defined unary
operator)... After all replacements due to macro expansion and the
defined unary operator have been performed, all remaining identifiers
are replaced with the pp-number 0, and then each preprocessing token is
converted into a token. ^^^^


Bookmark this website: http://dev.unicals.com/papers/c99-draft.html

Hit Ctrl-F and search away next time you need "proof" that the answers
your getting here to your questions are valid or not.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top