• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Increment decrement operators

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int the line

int i =x++ + ++x ; i = 4

because according to operator precedence ++x evaluates to 2 and then x++ again to 2 thus giving output i=4 and x = 3

but

int i = x++ + x++ the output is x=3 i=3
could some one explain me how it got evaluated to the above output.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i = x++ + x++;

i = 1 + 2 = 3 and x = 3.

Before evaluating the second x++ the value of x is 2, hence it takes the value 2 and not 1.

i = 1 + (2++)

Regards
Atul
 
reply
    Bookmark Topic Watch Topic
  • New Topic