Why?!?

This is a discussion on Why?!? within the C Programming forums, part of the General Programming Boards category; Hi everybody! I was testing a little program to see if I was using correctly some preprocessor directives and I ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    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...

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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)
    Last edited by carrotcake1029; 01-08-2009 at 11:25 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by p3rry View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    Thank You

    Ok, I got it.
    I thaught #define worked in a different way...so I also learned something new
    Thank you very much

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21