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. postfix operator problem

postfix operator problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
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.
  • P Offline
    P Offline
    P e t e r
    wrote on last edited by
    #1

    Following code works well when compiled with Microsoft c-compiler : j = 0; j = (j < 1) ? j++ : 0; --> result j = 1 When compiled with Microsoft c++ compiler --> result = 0 (wrong) Can somebody explain this?

    J M 2 Replies Last reply
    0
    • P P e t e r

      Following code works well when compiled with Microsoft c-compiler : j = 0; j = (j < 1) ? j++ : 0; --> result j = 1 When compiled with Microsoft c++ compiler --> result = 0 (wrong) Can somebody explain this?

      J Offline
      J Offline
      Jon
      wrote on last edited by
      #2

      0 is correct, use ++j if you want j to be incremented before being assigned back to itself. You get 0 because C++ create a temporary (j) variable and increments it (and discards it in this case), where as C doesn't bother (I think).

      1 Reply Last reply
      0
      • P P e t e r

        Following code works well when compiled with Microsoft c-compiler : j = 0; j = (j < 1) ? j++ : 0; --> result j = 1 When compiled with Microsoft c++ compiler --> result = 0 (wrong) Can somebody explain this?

        M Offline
        M Offline
        Mike Dunn
        wrote on last edited by
        #3

        Well, the correct answer is 0. The assignment reduces to: j = j++; Since j++ evaluates to 0, 0 gets stuffed in j. This is really bad programming practice, though. I hope you don't plan on writing production code this way. ;)

        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