Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

devendra_iiti Group Title

explain output of int x = 4; x = x + x + x++; printf("%d\n",x); x = 4; printf("%d\n",x + x + x++); output of above code is 13 12

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    x++ means: first read the current value of x and then increment its value. so in the statement: x = x + x + x++ what happens is as follows: x = 4 + 4 + 4 = 12 then the "post increment" on x is applied (i.e. x++) resulting in x getting the value 13 In the second statement: printf("%d\n",x + x + x++); the same thing happens, except this time the result of "x + x + x++" is printed out first and then the current value of x is incremented - which in this case would mean x would end up with a value of 5.

    • 2 years ago
  2. devendra_iiti Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    it doesn't work on #define p(x) printf("%d\n",x); int x = 4; x = x + x++ + x; p(x); x = 4; p(x + x++ + x); same output 13 12

    • 2 years ago
  3. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    what do you mean by it doesn't work?

    • 2 years ago
  4. devendra_iiti Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    according to ur explanation first + is evaluated then increment but post increment has higher precedence than normal addition and assignment operator so it should be evaluated first

    • 2 years ago
  5. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    post increment always occurs AFTER the value has been read and processed

    • 2 years ago
  6. devendra_iiti Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks i get it.

    • 2 years ago
  7. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    yw :)

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.