preprocessor bug?

Discussion in 'C Programming' started by Gilles, May 10, 2013.

  1. Gilles

    Gilles Guest

    Hi,

    I'm trying to find a automated way of defining a macro, depending on another macro's value. More precisely, I try to define for "BAR" the larger power of 2 strictly smaller than "FOO", up to 2048.

    Here is my sample code:

    -------------8<--------------------------------------------------
    #include <stdio.h>

    #if FOO > 1024
    #define BAR 1024
    #elif FOO > 512
    #define BAR 512
    #elif F00 > 256
    #define BAR 256
    #elif FOO > 128
    #define BAR 128
    #elif FOO > 64
    #define BAR 64
    #elif F00 > 32
    #define BAR 32
    #elif FOO > 16
    #define BAR 16
    #elif FOO > 8
    #define BAR 8
    #elif FOO > 4
    #define BAR 4
    #elif FOO > 2
    #define BAR 2
    #else
    #define BAR 1
    #endif

    int main(int argc, char *argv[]) {
    printf("FOO: %d BAR: %d\n", FOO, BAR);
    return 0;
    }
    -------------8<--------------------------------------------------

    The code, albeit not especially pretty, does look quite straightforward to me. However, when I try it, I get strange results:

    gilles@localhost:~/tmp$ gcc -DFOO=513 foo.c && ./a.out
    FOO: 513 BAR: 512
    gilles@localhost:~/tmp$ gcc -DFOO=512 foo.c && ./a.out
    FOO: 512 BAR: 128

    The first line is what I expected, but the second is not! This should have given 256 for BAR.
    Do I miss something obvious or is that a preprocessor bug?

    Just for information, system is Ubuntu 12.04 and gcc version is 4.6.3.

    Thanks for any help.

    Gilles
     
    Gilles, May 10, 2013
    #1
    1. Advertisements

  2. Gilles

    Gilles Guest

    On 10/05/13 08:47, Dr Nick wrote:
    > Gilles <> writes:
    >> I try to define for "BAR" the larger power of 2 strictly smaller than "FOO", up to 2048.

    [snip]
    >> Do I miss something obvious or is that a preprocessor bug?

    >
    > You missed something very obvious indeed - prepare to kick yourself!
    >
    > Your code says
    >> #elif FOO > 512
    >> #define BAR 512

    >
    > And 512 is /not/ greater than 512...
    >


    Yes, 512 is not greater than 512, so I expect BAR to be 256. But I get 128, hence my posting here.
     
    Gilles, May 10, 2013
    #2
    1. Advertisements

  3. Gilles

    Joe Pfeiffer Guest

    Gilles <> writes:

    > Hi,
    >
    > I'm trying to find a automated way of defining a macro, depending on another macro's value. More precisely, I try to define for "BAR" the larger power of 2 strictly smaller than "FOO", up to 2048.
    >
    > Here is my sample code:
    >
    > -------------8<--------------------------------------------------
    > #include <stdio.h>
    >
    > #if FOO > 1024
    > #define BAR 1024
    > #elif FOO > 512
    > #define BAR 512
    > #elif F00 > 256
    > #define BAR 256
    > #elif FOO > 128
    > #define BAR 128


    You compare FOO (that's eff oh oh) against 512, but F00 (that's eff zero
    zero) against 256.
     
    Joe Pfeiffer, May 10, 2013
    #3
    1. Advertisements

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

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    807
  2. RedEye
    Replies:
    2
    Views:
    758
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    885
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    1,094
    tom_usenet
    Jun 27, 2003
  5. Cronus
    Replies:
    1
    Views:
    932
    Paul Mensonides
    Jul 15, 2004
  6. Steve Holden
    Replies:
    1
    Views:
    599
    Behrang Dadsetan
    Jul 2, 2003
  7. Chris Green
    Replies:
    2
    Views:
    537
    Michael Schutte
    Dec 18, 2003
  8. Subra
    Replies:
    7
    Views:
    547
    Subra
    Jul 4, 2007
Loading...