473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Using the % operator with preprocessor constants

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

Mar 8 '06 #1
2 1248
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
Mar 8 '06 #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

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...
Mar 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
14
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time...
5
by: Veit Wiessner | last post by:
I wrote a program that handles the buildcount of projects (gets called every time I compile a project, it writes a header file which is #include-ed in the project). My question is this, is it...
6
by: francesco | last post by:
I'm programming an embedded system, on a Fujitsu microcontroller. Stated that I'm not sure about constants optimization, I'm trying to declare constants in two ways, one for debug version and one...
3
by: Wessi | last post by:
Hi, token pasting means, that normally whitespaces and comments are deleted before and after the ## operator. I want the preprocessor to hold an existing whitespace while replacing the argument...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
2
by: merkury1 | last post by:
Does C# have any predefined preprocessor constants like the C/C++ compiler does? In C++ you had such predefined constants as: __cplusplus, _DEBUG, _WIN32, MSC_VER. Are there any such things for...
36
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index ==...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.