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

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

  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. 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. Chris P
    Replies:
    0
    Views:
    441
    Chris P
    Oct 28, 2003
  2. The Weiss Family

    VHDL Preprocessor

    The Weiss Family, Jul 14, 2004, in forum: VHDL
    Replies:
    2
    Views:
    3,228
    The Weiss Family
    Jul 14, 2004
  3. =?Utf-8?B?SSBhbSBTYW0=?=

    C# Preprocessor

    =?Utf-8?B?SSBhbSBTYW0=?=, Mar 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,580
    =?Utf-8?B?SSBhbSBTYW0=?=
    Mar 13, 2005
  4. Cronus
    Replies:
    1
    Views:
    677
    Paul Mensonides
    Jul 15, 2004
  5. Subra
    Replies:
    7
    Views:
    433
    Subra
    Jul 4, 2007
Loading...

Share This Page