# Why?!?

• 01-08-2009
p3rry
Why?!?
Hi everybody!
I was testing a little program to see if I was using correctly some preprocessor directives and I got something unexpected.
Here's the code
Code:

```#include <stdio.h> #include <stdlib.h> #define Lato 16 #define N 3 #define Rmin 2 #define Rmax 8 #if (Rmin%2 == 0 && Rmax%2 == 0) #define Ymin Rmin/2 #define Ymax Rmax/2 #define Remin Rmin #define Remax Rmax #define Romin Rmin+1 #define Romax Rmax-1 #elif (Rmin%2 != 0 && Rmax%2 == 0) #define Ymin (Rmin-1)/2 #define Ymax Rmax/2 #define Remin Rmin+1 #define Remax Rmax #define Romin Rmin #define Romax Rmax-1 #elif (Rmin%2 != 0 && Rmax%2 != 0) #define Ymin (Rmin-1)/2 #define Ymax (Rmax+1)/2 #define Remin Rmin+1 #define Remax Rmax-1 #define Romin Rmin #define Romax Rmax #else #define Ymin Rmin/2 #define Ymax (Rmax+1)/2 #define Remin Rmin #define Remax Rmax-1 #define Romin Rmin+1 #define Romax Rmax #endif #define  R Ymax-Ymin+1 #define  Dodd (Romax-Romin)/2+1 #define  Deven (Remax-Remin)/2+1   int main(){ //Then I print all   printf("\nYmin=\t%d\tYmax=\t%d\nRemin=\t%d\tRemax=\t%d\nRomin=\t%d\tRomax=\t%d\nR=\t%d\nDodd=\t%d\tDeven\t%d\n",Ymin,Ymax,Remin,Remax,Romin,Romax,R,Dodd,Deven);     return 0; }```
Ok, here's what I got on the terminal when I run it

Ymin= 1 Ymax= 4
Remin= 2 Remax= 8
Romin= 3 Romax= 7
R= 4
Dodd= 4 Deven= 4

Can you explain me why Dodd= (Romax-Romin)/2+1 and Deven= (Remax-Remin)/2+1 are both equal to 4?!?
Thank you very much...
• 01-08-2009
carrotcake1029
Edit: scratch what I have said. Change the damn variable names, I keep getting lost.

Ughh found it, you need parentheses
Code:

```#define Romin (Rmin+1) #define Romax (Rmax-1)```
• 01-08-2009
matsp
Quote:

Originally Posted by p3rry
Hi everybody!
I was testing a little program to see if I was using correctly some preprocessor directives and I got something unexpected.
Here's the code
Code:

```#include <stdio.h> #include <stdlib.h> #define Lato 16 #define N 3 #define Rmin 2 #define Rmax 8 #if (Rmin%2 == 0 && Rmax%2 == 0) #define Ymin Rmin/2 #define Ymax Rmax/2 #define Remin Rmin #define Remax Rmax #define Romin Rmin+1 #define Romax Rmax-1 #elif (Rmin%2 != 0 && Rmax%2 == 0) #define Ymin (Rmin-1)/2 #define Ymax Rmax/2 #define Remin Rmin+1 #define Remax Rmax #define Romin Rmin #define Romax Rmax-1 #elif (Rmin%2 != 0 && Rmax%2 != 0) #define Ymin (Rmin-1)/2 #define Ymax (Rmax+1)/2 #define Remin Rmin+1 #define Remax Rmax-1 #define Romin Rmin #define Romax Rmax #else #define Ymin Rmin/2 #define Ymax (Rmax+1)/2 #define Remin Rmin #define Remax Rmax-1 #define Romin Rmin+1 #define Romax Rmax #endif #define  R Ymax-Ymin+1 #define  Dodd (Romax-Romin)/2+1 #define  Deven (Remax-Remin)/2+1   int main(){ //Then I print all   printf("\nYmin=\t%d\tYmax=\t%d\nRemin=\t%d\tRemax=\t%d\nRomin=\t%d\tRomax=\t%d\nR=\t%d\nDodd=\t%d\tDeven\t%d\n", Ymin,Ymax,Remin,Remax,Romin,Romax,R,Dodd,Deven);     return 0; }```
Ok, here's what I got on the terminal when I run it

Ymin= 1 Ymax= 4
Remin= 2 Remax= 8
Romin= 3 Romax= 7
R= 4
Dodd= 4 Deven= 4

Can you explain me why Dodd= (Romax-Romin)/2+1 and Deven= (Remax-Remin)/2+1 are both equal to 4?!?
Thank you very much...

I marked the area that gets compiled as red above:

The printf line's argument turns into (according to gcc -E):
Code:

`2/2, 8/2, 2, 8, 2 +1, 8 -1, 8/2 -2/2 +1, (8 -1 -2 +1)/2+1, (8 -2)/2+1`
I think you'll find that you need some extra parenthesis on the Romax, Romin.

--
Mats
• 01-08-2009
p3rry
Thank You
Ok, I got it.
I thaught #define worked in a different way...so I also learned something new
Thank you very much