Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Increment and decrement operator

Vishal ShuklaVishal Shukla Member Posts: 2

Int main()
{
a=10;
a=a++;
a=a++ * a--;
print("%d",a);
}
I don't get why the output is 110 plz explain the output

Comments

  • B00B00 Member Posts: 94 ✭✭✭

    First off, your code is not even valid C.
    Now translation of your code:

    a = 10
    a = 10
    a = 11 * 10
    

    For why that is go read some about sequence point.

  • Vishal ShuklaVishal Shukla Member Posts: 2

    Hey boo ,sorry I didn't mention that a is int data type, but u didn't explain it well why in last line it's 11*10

  • B00B00 Member Posts: 94 ✭✭✭

    What do you expect it to be? On line 2 assignment precedes the increment. On line 3 it's an expression so increment/decrement need to be evaluated first before both numbers are multiplied.
    Maybe this will be more clear:

    a = 10
    (a = a + 1) * (a = a - 1) // a++ * a--
    
  • Priyasha SarmahPriyasha Sarmah Member Posts: 14

    Assignment operator preceds the increment. Moreover, the value of a is initialised to be 10 and then multiplied with 11 which gives the result 110. For help on operators in C, follow the link : http://knowledgetpoint.com/csharp/csharp-language-fundamentals/

Sign In or Register to comment.