Thread: macro definitions problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    21

    macro definitions problem

    Code:
    #include <stdio.h>
    #define next(x) x+1
    #define max(x,y) x >= y ? x : y
    
    
    int a = ..;
    int b = ..;
    void main(void) {
    
    
    printf("%d ", max(next(a)*2,++b));
    printf("%d %d\n", a, b);
    
    
    }
    Lets say we have inputs a=6 and b=2:

    max(next(a)*2,3) ----> max(6+1*2,3) ----> max(8,3) ---> 8

    Output: 8 6 3 (which is correct)

    However, if we have inputs a=4 and b=7:

    max(next(4)*2,8) ----> max(4+1*2,8) -----> max(6,8) ----8

    Output: 8 4 8

    But when I run this code for this input I get "9 4 8". Where did I go wrong?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Expand the macro by hand, on paper. Take a careful look at the results, then take a read through this: Question 3.8.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    I have one more of these:

    Code:
    #include <stdio.h>
    
    
    #define min(a,b)  ((a) > (b) ? (b) : (a))
    #define inc(a)    a++
    #define mult(a,b) (a * b)
    
    
    int main(void) {
    
        int x = ..;
        int y = ..;
        
        printf("min(%d,inc(%d))",x,y);
        printf("=%d\n",min(x,inc(y)));
        printf("min(mult(%d,%d+2),11)",x,y);
        printf("=%d\n",min(mult(x,y+2),11));
        return 0;
    
    
    }

    For inputs x=2 and y=3:

    min(2,inc(3)) ---> min(2,3++) ---> min(2,4) ---> 2

    (note y is now 4)

    min(mult(2,4+2),11) ---> min(mult(2,6),11) ---> min(12,11) ---> 10

    Output:

    min(2,inc(3))=2
    min(mult(2,4+2),11)=10

    However for negative numbers say, x=-1 and y=-2 I get a strange result:

    min(-1,inc(-2)) ---> min(-1,-2++) ---> min(-1,-1) ---> -1

    y is now -1

    min(mult(-1,-1+2),11) ---> min(mult(-1,1),11) ---> min(-1,11) ---> -1

    Output:
    min(2,inc(3))=-1
    min(mult(2,4+2),11)=-1

    But this is incorrect, the output is actually supposed to be -1 and 2.

    If you run the code for the above inputs it shows "min(mult(-1,0+2),11)". Some how y has been incremented twice to get zero, not sure how this has happened.
    Last edited by acpower; 06-14-2012 at 12:34 PM.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    @anduril462: I've already tried this by hand, first thing I did. And I've made many attempts, ran the code etc

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    955
    If you actually expanded the macros by hand, you would have seen the problem:
    Code:
    min(x,inc(y))
    // expands to
    min(x,y++)
    // expands to
    ((x) > (y++) ? (y++) : (x))
    y is incremented twice because y++ appears twice. Do the same expansion by hand with your mult() macro and you'll see the problem when you try to do something like "mult(x,y+2)".

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    oops thanks!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Or you can get the compiler to show you what the pre-processor has done.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    #define min(a,b)  ((a) > (b) ? (b) : (a))
    #define inc(a)    a++
    #define mult(a,b) (a * b)
    
    int main(void) {
        int x = 0;
        int y = 0;
        printf("min(%d,inc(%d))",x,y);
        printf("=%d\n",min(x,inc(y)));
        printf("min(mult(%d,%d+2),11)",x,y);
        printf("=%d\n",min(mult(x,y+2),11));
        return 0;
    }
    $ gcc -E foo.c | tail 
    
    int main(void) {
        int x = 0;
        int y = 0;
        printf("min(%d,inc(%d))",x,y);
        printf("=%d\n",((x) > (y++) ? (y++) : (x)));
        printf("min(mult(%d,%d+2),11)",x,y);
        printf("=%d\n",(((x * y+2)) > (11) ? (11) : ((x * y+2))));
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    21
    Hang on, if y was incremented twice how come for inputs x=2,y=3, y is 4 and not 5? Why was y incremented twice for y=-2 (to get zero) but not for y=3?
    Last edited by acpower; 06-15-2012 at 12:47 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by acpower
    if y was incremented twice how come for inputs x=2,y=3, y is 4 and not 5? Why was y incremented twice for y=-2 (to get zero) but not for y=3?
    Because (x) is then evaluated, not (y++), since 2 > 3 is false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 03-07-2011, 05:28 PM
  2. Macro Definitions and Usage
    By edunia11 in forum C Programming
    Replies: 12
    Last Post: 08-17-2006, 09:11 PM
  3. Replies: 13
    Last Post: 02-20-2005, 12:44 AM
  4. Local funtion definitions are illegal problem
    By Kaashead in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2004, 11:55 AM