4

I have a problem understanding the output of the code. Any explanation please...

#include<stdio.h>
void main()
{
     int x=2,y=5;
     x*=y+1;
     printf("%d",x);
}

The output is as 12. But as per my understanding x*=y+1;is x=x*y+1; but as per operator precedence x*y should be evaluated followed by adding 1 so it should be 10+1=11. But it is 12 — can anyone explain please?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shashank
  • 400
  • 8
  • 25
  • which compiler r u using ? Use GCC. – Aashu Aug 01 '14 at 06:05
  • 4
    It's `int main(void)`, not `void main()`. – Keith Thompson Aug 01 '14 at 06:06
  • @Aashu: It doesn't matter which compiler he's using. The code is behaving correctly, and no compiler will get this wrong. – Keith Thompson Aug 01 '14 at 06:06
  • 2
    @KeithThompson: except on Windows, and given the lack of newline at the end of the `printf()`, that's probably where it is being run. Yes, it should be `int main(void)`, but MS does allow `void main()`. – Jonathan Leffler Aug 01 '14 at 06:07
  • 2
    This is a duplicate question; the difficulty is finding the (best) duplicate. – Jonathan Leffler Aug 01 '14 at 06:07
  • @JonathanLeffler: True -- but I didn't *quite* say that `void main()` is incorrect. It's ugly and its mother dresses it funny, and all right-thinking individuals should shun it, but it's not incorrect. – Keith Thompson Aug 01 '14 at 06:09
  • That was quick, already 7 answers ... you can check this link for operators priority/precedence: http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm – smagnan Aug 01 '14 at 06:09
  • See: [What does the `|=` operator mean in C++](http://stackoverflow.com/questions/4217762/what-does-the-operator-mean-in-c/4217772#4217772) for an exposition on why the compound assignment operators are useful. However, this question isn't quite an exact duplicate of that one, though it is close (all else apart, the language is officially different — C++ vs C — but the emphasis is also slightly different). – Jonathan Leffler Aug 01 '14 at 06:18

9 Answers9

13

It will be evaluated as

x = x * (y + 1);

so

x = 2 * ( 5 + 1 )
x = 12
sujithvm
  • 2,351
  • 3
  • 15
  • 16
10

What's going on here is how the order of operations happens in programming.

Yes, if you were to have this equation x*y+1 it would be (x * y ) + 1 and result in eleven.

But in programming, the equation to the right of the = sign is solved for prior to being modified by the symbol proceeding the = sign. In this equation it is multiplied.

So x *= y + 1 is actually x = x * ( y + 1 ) which would be 12.
^ In this case, the asterisk(*) is multiplying the entire equation on the right hand side by x and then assigning that outcome to x.

Andrew
  • 3,393
  • 4
  • 25
  • 43
2

It is translated into : x = x*(y+1);

So very obviously it prints out 12.

Krypton
  • 3,337
  • 5
  • 32
  • 52
1

Your understanding is correct but it's somthing like this:

x*=y+1;  =>  x = x * (y + 1);

Now apply BODMAS

AmanVirdi
  • 1,667
  • 2
  • 22
  • 32
1

x *= y + 1 is x = x * (y + 1)

Operator + has higher precedence than operator *=.

Alper
  • 12,860
  • 2
  • 31
  • 41
1

x*=y; works like x=x*y; and here x*=(y+1) is getting expanded like x = x * (y + 1);

nobalG
  • 4,544
  • 3
  • 34
  • 72
0

Here from operator procedure in c you can see

Addition/subtraction assignment has lower procedure than simply add operation.

so here

x*=y+1;

+ get executed first.

so

x = x * (6)

so x = 2 * 6

x = 12;
Chandru
  • 1,306
  • 13
  • 21
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

*= and similar ones are a type of C assignment operators, i.e. these operators are different from * and alike.

Now from C operator precedence, these operators have lowest precedence (higher than ,) hence y + 1 will be evaluated first, then *= will be evaluated and result will be assigned to x.

0xF1
  • 6,046
  • 2
  • 27
  • 50
0

It evaluates as

x = x * (y + 1);

so

x = 2 * (5 + 1) = 12

Take a look at Operators order, you will see why in this case it is evaluated like that.

ST3
  • 8,826
  • 3
  • 68
  • 92