Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

preprocessor bug?

42 views
Skip to first unread message

Gilles

unread,
May 10, 2013, 2:37:25 AM5/10/13
to
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

Dr Nick

unread,
May 10, 2013, 2:47:21 AM5/10/13
to
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...

Mark Bluemel

unread,
May 10, 2013, 2:51:51 AM5/10/13
to
On 10/05/2013 07:37, Gilles wrote:
> 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

The first two tests check "FOO"

> #elif F00 > 256
> #define BAR 256

This test checks "F00" - 'F' followed by two zeroes

> #elif FOO > 128
> #define BAR 128
> #elif FOO > 64
> #define BAR 64

These tested "FOO"

> #elif F00 > 32
> #define BAR 32

What did this test?

etc...

Was this a rather odd troll? I find it hard to see how you'd mistype
this way.

Gilles

unread,
May 10, 2013, 2:51:58 AM5/10/13
to
On 10/05/13 08:47, Dr Nick wrote:
> Gilles <gil...@foo.invalid> 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

unread,
May 10, 2013, 2:56:37 AM5/10/13
to
Many thanks, this was driving me crazy!
Well, I do this sort of typo all the time, since O and 0 are just one above the other on my keyboard. I should change my font I guess to get a zero more like an empty ensemble...
Really, you made my day, thanks.

Gilles

Joe Pfeiffer

unread,
May 10, 2013, 3:26:13 PM5/10/13
to
Gilles <gil...@foo.invalid> 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.
0 new messages