Using sizeof in the preprocessor

Discussion in 'C++' started by Johannes Schaub, May 21, 2011.

  1. I have this code in my pet project

    #if sizeof -1 > 4
    #error "This doesn't work on 64bit"
    #endif

    My friend was telling me that this shouldn't work because "sizeof is not
    evaluated in the preprocessor". Yet the compiler accepts it and my program
    works.

    Can anybody please explain this? Is it a compiler bug? Thanks to all!
     
    Johannes Schaub, May 21, 2011
    #1
    1. Advertising

  2. Johannes Schaub

    Huibert Bol Guest

    Johannes Schaub wrote:

    > I have this code in my pet project
    >
    > #if sizeof -1 > 4
    > #error "This doesn't work on 64bit"
    > #endif
    >
    > My friend was telling me that this shouldn't work because "sizeof is not
    > evaluated in the preprocessor". Yet the compiler accepts it and my program
    > works.


    Identifiers in #if expressions are evaluated as zero, so the expression
    is equivalent to

    #if 0 -1 > 4

    which is never true.

    --
    Huibert
    "Okay... really not something I needed to see." --Raven
     
    Huibert Bol, May 21, 2011
    #2
    1. Advertising

  3. Huibert Bol <> wrote:
    > Identifiers in #if expressions are evaluated as zero


    It seems that at least with gcc "#if true" and "#if false" work as one
    would expect. Is this standard?
     
    Juha Nieminen, May 21, 2011
    #3
  4. Johannes Schaub

    Huibert Bol Guest

    Juha Nieminen wrote:

    > Huibert Bol <> wrote:
    >> Identifiers in #if expressions are evaluated as zero

    >
    > It seems that at least with gcc "#if true" and "#if false" work as one
    > would expect. Is this standard?


    Yes, "except true and false". Also the alternative tokens (and, andeq,
    bitand, etc, ...) are replaced as they are not considered identifiers.

    --
    Huibert
    "Okay... really not something I needed to see." --Raven
     
    Huibert Bol, May 21, 2011
    #4
  5. Juha Nieminen wrote:

    > Huibert Bol <> wrote:
    >> Identifiers in #if expressions are evaluated as zero

    >
    > It seems that at least with gcc "#if true" and "#if false" work as one
    > would expect. Is this standard?


    There are explicit exceptions for true and false. The spec says

    After all replacements due to macro expansion and the defined unary operator
    have been performed, all remaining identi├»┬Čers and keywords, except for true
    and false, are replaced with the pp-number 0, and then each preprocessing
    token is converted into a token. The resulting tokens comprise the
    controlling constant expression which is evaluated according to the rules of
    5.19 ...
     
    Johannes Schaub, May 21, 2011
    #5
  6. Juha Nieminen <> writes:

    > Huibert Bol <> wrote:
    >> Identifiers in #if expressions are evaluated as zero

    >
    > It seems that at least with gcc "#if true" and "#if false" work as one
    > would expect. Is this standard?


    Funny idea. Here (gcc/g++ 4.4.4), gcc and g++ both with -E give
    different results on the following input:

    #if true
    #error "then"
    #else
    #error "else"
    #endif

    -- Alain.
     
    Alain Ketterlin, May 21, 2011
    #6
  7. Johannes Schaub wrote:

    > I have this code in my pet project
    >
    > #if sizeof -1 > 4
    > #error "This doesn't work on 64bit"
    > #endif
    >
    > My friend was telling me that this shouldn't work because "sizeof is not
    > evaluated in the preprocessor". Yet the compiler accepts it and my program
    > works.
    >
    > Can anybody please explain this? Is it a compiler bug? Thanks to all!


    I want to emphasize that I do *not* intent to troll. I wanted to raise a
    common issue and put people in alert mode when they see such code and don't
    get a compiler error.

    See http://chat.stackoverflow.com/transcript/message/752965#752965 and the
    following rage of "FredNurk" for the discussion and why I need to defend
    this question like that.
     
    Johannes Schaub, May 21, 2011
    #7
  8. Alain Ketterlin <-strasbg.fr> wrote:
    > Funny idea. Here (gcc/g++ 4.4.4), gcc and g++ both with -E give
    > different results on the following input:
    >
    > #if true
    > #error "then"
    > #else
    > #error "else"
    > #endif


    #if true
    std::cout << "You rock!\n"
    #else
    #error "Your language is too old. Please upgrade."
    #endif
     
    Juha Nieminen, May 22, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    sizeof in preprocessor

    Guest, Apr 3, 2004, in forum: C++
    Replies:
    5
    Views:
    12,331
    Jeff Schwab
    Apr 6, 2004
  2. Cronus
    Replies:
    1
    Views:
    699
    Paul Mensonides
    Jul 15, 2004
  3. Derek
    Replies:
    7
    Views:
    24,395
    Ron Natalie
    Oct 14, 2004
  4. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    655
    CBFalconer
    Apr 10, 2004
  5. Amigo

    Preprocessor and sizeof()

    Amigo, Sep 29, 2006, in forum: C Programming
    Replies:
    6
    Views:
    2,273
Loading...

Share This Page