473,387 Members | 3,684 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,387 software developers and data experts.

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

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

Nov 14 '05 #1
6 1720
ma*********************@yahoo.com wrote:
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

--
Peter

Nov 14 '05 #2
ma*********************@yahoo.com wrote:
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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3
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?

Nov 14 '05 #4
ma*********************@yahoo.com wrote:
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.

Nov 14 '05 #5
In article <11*********************@o13g2000cwo.googlegroups. com>
<ma*********************@yahoo.com> wrote:
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6

Thank you all very much. Mark

Nov 14 '05 #7

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

Similar topics

8
by: Erik Cato | last post by:
Hi group! Is this code legal? typedef enum { FALSE = 0, TRUE = 1, }t_bool;
24
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to...
14
by: genus_ | last post by:
The below code compiles and runs smoothly. Can any one at least explain to me what its doing for first few lines? Thank you. Here's the code: #include <stdio.h> main(t,_,a) char *a;...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: key9 | last post by:
Hi all: app error : //main.cpp .... char* q ; *q = 'c'; LinuxTestTerminal* lt = new LinuxTestTerminal(); lt->printch(q);
10
by: fusillo | last post by:
Hi, i've tried a test code about the Event Listener API but i've some problem about detaching the element firing the event with IE. Here's my code: <html> <head> <style type="text/css">...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.