4

I am running the following C code:

#define cube(x) (x*x*x)
void main()
{   
    int x=2,y;   
    y=cube(++x);            
    printf("%d %d",++x,y);    
}

I am expecting result as

6,60

But it is giving different result. I think I have misconception on pre-processor. I think the code will be similar to

void main()
{   
    int x=2,y;   
    y=++x*++x*++x;            
    printf("%d %d",++x,y);    
}

Please correct me if I am wrong.

I am interpretting the result to come as 3*4*5=60
but it is coming 125

Jainendra
  • 24,713
  • 30
  • 122
  • 169
  • 4
    http://c-faq.com/expr/seqpoints.html – cnicutar Apr 26 '12 at 08:35
  • Can you explain *why* you expect that result? It sounds like you have a misconception about the C language because there is no reason to expect any particular result. (Perhaps you're confusing the defined order in which the multiplication takes places with the unspecified order in which the things to multiply are themselves computed?) – David Schwartz Apr 26 '12 at 08:38
  • Am I missing something, When I run the above code (first one with preprocessor used) in Mac OSX Lion, with gcc version 4.2.1, I am getting value 6,60 – Krishnabhadra Apr 26 '12 at 09:48
  • @DavidSchwartz: As I know that prefix increment is right to left associative, so ++x*++x*++x i.e. 3*4*5 it will give 60. – Jainendra Apr 26 '12 at 10:04
  • @SachinMhetre: I got 6,125 as output – Jainendra Apr 26 '12 at 10:09
  • See http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Frerich Raabe Apr 26 '12 at 10:38
  • @Jaguar: You are assuming the parameters are evaluated from left to right. That is not required. For example, if the compiler evaluates all three parameters at the same time, it will be 3*3*3. Alternatively, all the pre-increments could be done first, yielding 5*5*5. Or something even weirder. – David Schwartz Apr 26 '12 at 10:42

4 Answers4

5

You defined a macro, which works as a simple string substitution, so that the presented translation is correct.

However, the order of execution of subexpressions is undefined and they can be, for example, interleaved and this makes the undefined behaviour.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • The variable `x` is modified more than once between sequence points, which is what causes the undefined behavior. While the order of evaluation of sub-expressions is undefined, that in it self doesn't necessarily cause undefined behavior (`x*x*x` is perfectly defined eg.). – Sander De Dycker Apr 26 '12 at 10:11
-1

The preprocessor take the expression what we are giving, so that, for your c code, initially x=2, so that while executing macro,

First it takes two values to multiply it, then takes third value multiply with the result, so the sequence should be,

++x * ++x => 3(first increment), 4(again for next increment), so now x value is 4, according to expression

4 * 4 => 16,

Again for next increment x => 5, so according to your expression result should be,

16 * 5 => 80

So now total result of multiplication is 80, and the x value is 5.

Mohanraj
  • 4,056
  • 2
  • 22
  • 24
  • The output in this case is coming 5*5*5=125 and value of x as 6 – Jainendra Apr 26 '12 at 10:08
  • No no, i just explained how the values incremented and how multiplication happen, dont take the last x value. Clearly i mentioned the result is 4*4*5 => 80, the x value is 5. If u increment x after this operation x value should be 6. – Mohanraj Apr 26 '12 at 10:12
  • I compiled that program the output is 125.(mohan raj its not 80). @Mohanraj which compiler u r using ...have u verified ur output on ur compiler? – USER_NAME Apr 26 '12 at 10:20
  • I am using ubuntu 11.10 and GCC compiler, my wholw program is #include #define cube(v) (v*v*v) void main() { int x=2,y; printf("%d \n",x); printf("%d \n", cube(++x)); printf("%d \n",x); } 2 80 5 and my output is – Mohanraj Apr 26 '12 at 10:23
  • @Jaguar are u in c chat room. – Mohanraj Apr 26 '12 at 10:28
  • [Undefined Behavior](http://stackoverflow.com/questions/3812850/output-of-multiple-post-and-pre-increments-in-one-statement) – Pavan Manjunath Apr 26 '12 at 11:00
-1

Output of this program differs with compilers, operating systems and even with the different versions of same compiler because output depends on the order of execution of sub-expressions.
I compiled it with Visual Studio: o/p is 125.
When I compiled with gcc: o/p is 80.
So you can not predict the output.
And I dont know why people ask these type of questions(those dont have a defined out come) in interviews?

USER_NAME
  • 1,029
  • 1
  • 14
  • 33
-1

The expression ++x*++x*++x will increment the value of x thrice i.e. the value of x would be 5 that is assigned to the expression. so 5*5*5 = 125 is the output. Again the value differs depending upon the architecture of the compiler.

Jainendra
  • 24,713
  • 30
  • 122
  • 169