Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #if constant expression

Reply
Thread Tools

#if constant expression

 
 
kerravon
Guest
Posts: n/a
 
      12-10-2007
According to 6.8 of C90, #if takes a constant expression.

According to 6.4 of C90, the sizeof operator is part of a constant
expression.

Why then do most of my compilers barf on this?

#include <stdio.h>

#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif

int main(void)
{
printf("hello %s\n", XXX);
return (0);
}

They barf on the "#if sizeof ..." line.

They explicitly say that I can't use sizeof in a #if

So which bit of the standard did I miss?

BFN. Paul.
 
Reply With Quote
 
 
 
 
vicks
Guest
Posts: n/a
 
      12-10-2007
> #include <stdio.h>
>
> #if sizeof(int) >= 4
> #define XXX "big"
> #else
> #define XXX "small"
> #endif
>
> int main(void)
> {
> printf("hello %s\n", XXX);
> return (0);
>
> }
>

[code snnipet...]
> So which bit of the standard did I miss?



Well I think that #if can be used only with the Macros(who gets
replaced with contant expressions at compile time) since sizeof gets
resolved at run time I dont think it's good idea to use sizeof with
#if!!

Regards
Vikas Gupta
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-10-2007
kerravon wrote:
> According to 6.8 of C90, #if takes a constant expression.
>
> According to 6.4 of C90, the sizeof operator is part of a constant
> expression.
>
> Why then do most of my compilers barf on this?
>
> #include <stdio.h>
>
> #if sizeof(int) >= 4
> #define XXX "big"
> #else
> #define XXX "small"
> #endif
>
> int main(void)
> {
> printf("hello %s\n", XXX);
> return (0);
> }
>
> They barf on the "#if sizeof ..." line.
>
> They explicitly say that I can't use sizeof in a #if
>
> So which bit of the standard did I miss?
>
> BFN. Paul.


I don't have a copy of C90; but I suspect that this is not an area where
the differences between C90 and C99 matter. Section 6.10.1p4 of
n1256.pdf says

"After all replacements due to macro expansion and the *defined* unary
operator have been performed, all remaining identifiers (including those
lexically identical to keywords) are replaced with the pp-number 0, "

I believe that this applies to both 'sizeof' and 'int' in your example.
As a result, it becomes:

#if 0(0) >= 4

which explains why it fails.
 
Reply With Quote
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      12-10-2007
vicks <(E-Mail Removed)> wrote in news:d485a4a6-dd4a-492e-a00b-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> Well I think that #if can be used only with the Macros(who gets
> replaced with contant expressions at compile time) since sizeof gets
> resolved at run time I dont think it's good idea to use sizeof with
> #if!!



sizeof yields a compile-time constant, and is very much _not_ evaluated at
runtime.

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-10-2007
kerravon wrote:
>
> According to 6.8 of C90, #if takes a constant expression.
>
> According to 6.4 of C90, the sizeof operator is part of a constant
> expression.


The preprocessor can't read keywords.


ISO/IEC 9899: 1990
6.8.1 Conditional inclusion
Constraints

identifiers (including those lexically identical to keywords)
are interpreted as described below;83

83 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.


--
pete
 
Reply With Quote
 
quarkLore
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 8:11 am, pete <(E-Mail Removed)> wrote:
> kerravon wrote:
>
> > According to 6.8 of C90, #if takes a constant expression.

>
> > According to 6.4 of C90, the sizeof operator is part of a constant
> > expression.

>
> The preprocessor can't read keywords.
>
> ISO/IEC 9899: 1990
> 6.8.1 Conditional inclusion
> Constraints
>
> identifiers (including those lexically identical to keywords)
> are interpreted as described below;83
>
> 83 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.
>
> --
> pete


#if is for preprocessor, sizeof is a compile time operator but not
handled at preprocessing stage. If you have gcc put sizeof in your
code and run gcc -E <soure filename> it should give you the
preprocessor output and sizeof would remain as it is.
 
Reply With Quote
 
Fancois Grieu
Guest
Posts: n/a
 
      12-10-2007
In article
<(E-Mail Removed)>,
kerravon <(E-Mail Removed)> wrote:

> Why then do most of my compilers barf on this?


#include <stdio.h>
#if sizeof(int) >= 4
#define XXX "big"
#else
#define XXX "small"
#endif
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}

One way to see this is that sizeof is not usable in the proprocessor, which has no
knowledge of types. There is a solution, though

#include <stdio.h>
#define XXX (sizeof(int) >= 4 ? "big" : "small")
int main(void)
{
printf("hello %s\n", XXX);
return (0);
}



If you want to check at compile time that int is big:


/* cause a compile-time error if x is 0 */
#define CompileAssert(x) do{typedef struct{char f[(x)?1:-1];}assertion_failure;}while(0) /* compile time assertion failure */

#include <stdio.h>
int main(void)
{
CompileAssert(sizeof(int) >= 4);
puts("hello big");
return (0);
}


Francois Grieu
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2007
pete <(E-Mail Removed)> writes:
> kerravon wrote:
>>
>> According to 6.8 of C90, #if takes a constant expression.
>>
>> According to 6.4 of C90, the sizeof operator is part of a constant
>> expression.

>
> The preprocessor can't read keywords.
>
>
> ISO/IEC 9899: 1990
> 6.8.1 Conditional inclusion
> Constraints
>
> identifiers (including those lexically identical to keywords)
> are interpreted as described below;83
>
> 83 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.


That quotation is from the C99 standard, not the C90 standard.

In both C90 and C99, handling of identifiers in a #if directive is
described as (C90 6.8.1, C99 6.10.1p3):

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.

Technical Corrigendum 3 (incorporated into n1256) changes this to:

After all replacements due to macro expansion and the defined
unary operator have been performed, all remaining identifiers

(including those lexically identical to keywords)

are replaced with the pp-number 0, and then each preprocessing
token is converted into a token.

I've marked the added text by putting it on a separate line.

This is a clarification, not a change, since keywords are lexically
identifiers. See DR #305,
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_305.htm>.
(It would be nice if the TC documents referred back to the DRs.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-10-2007
Keith Thompson wrote:
>
> pete <(E-Mail Removed)> writes:


> > ISO/IEC 9899: 1990
> > 6.8.1 Conditional inclusion
> > Constraints
> >
> > identifiers (including those lexically identical to keywords)
> > are interpreted as described below;83
> >
> > 83 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.

>
> That quotation is from the C99 standard, not the C90 standard.


That quotation is from the C90 standard.
I think you must be using a public draft instead.


This quotation is from the C99 standard:

ISO/IEC 9899:1999(E)
6.10.1 Conditional inclusion
Constraints

identifiers (including those lexically identical to keywords)
are interpreted as described below;140)

140) 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.

--
pete
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2007
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> pete <(E-Mail Removed)> writes:
>> > ISO/IEC 9899: 1990
>> > 6.8.1 Conditional inclusion
>> > Constraints
>> >
>> > identifiers (including those lexically identical to keywords)
>> > are interpreted as described below;83
>> >
>> > 83 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.

>>
>> That quotation is from the C99 standard, not the C90 standard.

>
> That quotation is from the C90 standard.
> I think you must be using a public draft instead.


No, I'm using a copy of the actual C90 standard, but you're right, the
quotation is from the C90 standard. I don't know how I missed that.
Apologies.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Case expression must be constant expression Philipp Java 26 11-25-2007 10:10 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments