Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How and why does the output vary for pre and post increment operators?

How and why does the output vary for pre and post increment operators?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 5495012
    wrote on last edited by
    #1

    How does the output vary for pre and post increment operators? In this code, how and why does value of 'a' vary? main() { int a=0; printf("%d\n",a++); printf("%d\n",++a); } OUTPUT: 0 2 main() { int a=0; printf("%d %d\n",a++,++a); } OUTPUT: 1,1

    _ T 2 Replies Last reply
    0
    • U User 5495012

      How does the output vary for pre and post increment operators? In this code, how and why does value of 'a' vary? main() { int a=0; printf("%d\n",a++); printf("%d\n",++a); } OUTPUT: 0 2 main() { int a=0; printf("%d %d\n",a++,++a); } OUTPUT: 1,1

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      main() { int a=0; printf("%d\n",a++); // POST INCREMENT a is still 0 // HERE a is 1 printf("%d\n",++a); // PRE INCREMENT a is (1+1) = 2 now } main() { int a=0; printf("%d %d\n",a++,++a); // Move from right to left // ++a is pre increment so value for a is 1 for ++a. a++ is post increment so the value still remains 1 for a++ // Here a would be 2 } Preincrement means the value is first operated upon and then used, while in post increment the value is first used then the change happens Hope this helps

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • U User 5495012

        How does the output vary for pre and post increment operators? In this code, how and why does value of 'a' vary? main() { int a=0; printf("%d\n",a++); printf("%d\n",++a); } OUTPUT: 0 2 main() { int a=0; printf("%d %d\n",a++,++a); } OUTPUT: 1,1

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        the developer shouldn't rely on the order of the parameters. your inline printf() (the second example) is really bad. On Visual C++, the parameters are unstacked from right to left, so when you do

        printf("%d %d\n",a++,++a)

        ++a is evaluated before a++. this may be different on other compilers. for how the value of a vary, you have to know how pre and post increment operators work. Preincrement (++a) increments the variable and returns its new value (the value after the incrementation). Postincrementation (a++) increments the variable ans returns its old value (the value before the incrementation).

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups