Using the % operator with preprocessor constants

Discussion in 'C++' started by zahy[dot]bnaya[At]gmail[dot]com, Mar 8, 2006.

  1. hello,
    I have a constant declared as

    #define TABLESIZE 16*15*14*13*12*11*10*9/3 /// 172972800

    I tried the following code:

    if (TABLESIZE == 172972800)
    {
    printf("%d\n",518763960%172972800);
    printf("%d\n",518763960%TABLESIZE);
    printf("%d\n",TABLESIZE);
    }



    My output is:

    172818360
    86486400
    172972800



    How can it be?
    Thanks
     
    zahy[dot]bnaya[At]gmail[dot]com, Mar 8, 2006
    #1
    1. Advertising

  2. zahy[dot]bnaya[At]gmail[dot]com wrote:
    > hello,
    > I have a constant declared as
    >
    > #define TABLESIZE 16*15*14*13*12*11*10*9/3 /// 172972800


    It's not a constant. It's a piece of text. If you need a constant, use

    const long TABLESIZE = ....

    > I tried the following code:
    >
    > if (TABLESIZE == 172972800)
    > {
    > printf("%d\n",518763960%172972800);
    > printf("%d\n",518763960%TABLESIZE);


    Substitute the contents of your 'TABLESIZE' macro here. What do you get?
    Write it down. Look what the '%' applies to.

    > printf("%d\n",TABLESIZE);
    > }
    >
    >
    >
    > My output is:
    >
    > 172818360
    > 86486400
    > 172972800
    >
    >
    >
    > How can it be?


    Do NOT use macros when you need a constant.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 8, 2006
    #2
    1. Advertising

  3. zahy[dot]bnaya[At]gmail[dot]com

    Ben Pope Guest

    zahy[dot]bnaya[At]gmail[dot]com wrote:
    > hello,
    > I have a constant declared as
    >
    > #define TABLESIZE 16*15*14*13*12*11*10*9/3 /// 172972800
    >
    > I tried the following code:
    >
    > if (TABLESIZE == 172972800)
    > {
    > printf("%d\n",518763960%172972800);
    > printf("%d\n",518763960%TABLESIZE);
    > printf("%d\n",TABLESIZE);
    > }
    >
    >
    >
    > My output is:
    >
    > 172818360
    > 86486400
    > 172972800
    >
    >
    >
    > How can it be?


    Macros are evil.

    Rule 1* is to always put it in brackets:
    #define TABLESIZE (16*15*14*13*12*11*10*9/3)

    Macros are text replacement so your second printf expands to:

    printf("%d\n",518763960%16*15*14*13*12*11*10*9/3);

    Rule one comes after at least 3 other rules, which essentially all say:
    Don't use macros!

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Mar 8, 2006
    #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. Cronus
    Replies:
    1
    Views:
    676
    Paul Mensonides
    Jul 15, 2004
  2. Veit Wiessner
    Replies:
    5
    Views:
    456
    Veit Wiessner
    Dec 3, 2003
  3. news.sunrise
    Replies:
    10
    Views:
    797
  4. mrstephengross
    Replies:
    8
    Views:
    667
    Jack Klein
    Oct 12, 2005
  5. Guenter Dannoritzer

    #, ## preprocessor operator weirdness with g++

    Guenter Dannoritzer, Nov 24, 2007, in forum: C++
    Replies:
    1
    Views:
    836
    Justin Spahr-Summers
    Nov 24, 2007
Loading...

Share This Page