Thread: program ouputs wierd results

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    program ouputs wierd results

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h> 
    
    #define CUBE(x) (x*x*x)
    
    
    int main(void)
    {
    int x, y;
    x = 5;
    y = CUBE(++x);
    printf("y is %d\n", y);
    }
    following code snipet:

    Ouputs : 392

    changing macro to

    Code:
    #define CUBE(x) (x*x)

    Ouputs : 49

    shouldn't this be 42......

    Using VC++ IDE

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Instead of ++x if you write x++ then it is giving 125 as a correct answer in the case of CUBE(x*x*x).
    And also in case of CUBE(x*x) it is giving the result as 25

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by thillai_selvan View Post
    Instead of ++x if you write x++ then it is giving 125 as a correct answer in the case of CUBE(x*x*x).
    And also in case of CUBE(x*x) it is giving the result as 25
    Thanks thillian....

    I was expecting an explanation for the behaviour..........

    Why the second snippet is taking 7*7 = 49?
    Why not 7*6 =42?

    Does it have something to do with the endiness?.....correct me if I am wrong?
    Last edited by sanddune008; 03-01-2010 at 03:26 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: program ouputs wierd results

    You can use the macro expansion, If you have cc compiler.
    you can use -E option to expand a macro.

    It will give like,
    Code:
    y = (++x*++x); //for y=CUBE(x);
    I think now you can understand clearly.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    In your program,your macro CUBE(x*x*x) will be expanded as follows.

    CUBE((x*x)*x)

    So,when your program passes (++x) as the input to this function.It will be expanded as follows.

    ((++x*++x)*++x)

    First,the value of x is incremented to 6 and then incremented to 7 because ++ operator has higher precedence than *.When the multiplication operation performs both the values will be 7*7.So,the result of it is 49 and then when the final increment of x.The value of x will be incremented to 8 and then it multiplied with 49.That's why,the result is 392.

    You run your code by removing one x in your CUBE(x*X),then it will give the result as 49.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Expressions with multiple side effects on the same variable (between sequence points) are ALWAYS UNDEFINED.
    Expressions

    You can't rationalise the result by () or precedence or anything else.
    The code is just broken.
    Everything you wanted to know about undefined behaviour (but were afraid to ask) - C

    Sooner or later, you'll find some compiler or option setting that will just ruin your carefully crafted assumptions about how such things are dealt with.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks Salem.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. triggering another program to run using serial port
    By infineonintern in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2009, 05:16 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 06-15-2003, 11:20 AM
  5. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM