Can the C preprocessor perform integer arithmetic?

Viewed 72189

As the questions says, is the C preprocessor able to do it?

E.g.:

#define PI 3.1416
#define OP PI/100
#define OP2 PI%100

Is there any way OP and/or OP2 get calculated in the preprocessing phase?

6 Answers

Be carefull when doing arithmetic: add parenthesis.

#define SIZE4 4
#define SIZE8 8
#define TOTALSIZE SIZE4 + SIZE8

If you ever use something like:

unsigned int i = TOTALSIZE/4;

and expect i to be 3, you would get 4 + 2 = 6 instead. Add parenthesis:

#define TOTALSIZE (SIZE4 + SIZE8)
Related