Like Tree3Likes
  • 3 Post By Salem

How does compiler solve this expression

This is a discussion on How does compiler solve this expression within the C Programming forums, part of the General Programming Boards category; The expressions and their output as seen on Codeblocks 12.11 are - int i = 1, x; x = i++ ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    18

    How does compiler solve this expression

    The expressions and their output as seen on Codeblocks 12.11 are -

    int i = 1, x;
    x = i++ + i++ + ++i ++i
    output : x = 7 and i = 5;

    But for the following -

    int i = 1, x;
    x = ++i ++i + i++ + i++
    output : x = 12 and i = 5;

    Can anyone be kind enough to help me know how to solve these types of expressions correctly ?
    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,446
    > Can anyone be kind enough to help me know how to solve these types of expressions correctly ?
    Yes, you rewrite them so they no longer exhibit undefined behaviour.

    And read this -> Post and Pre Increment
    Learn that such code has no value because there is no way to figure out what any given compiler is going to do with it (because it's UNDEFINED BEHAVIOUR).

    If your tutor has trouble with this, just send them over here for some re-education.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 04:19 PM
  2. Replies: 2
    Last Post: 11-25-2009, 06:38 AM
  3. Replies: 2
    Last Post: 02-04-2008, 01:34 AM
  4. Expected Constant Expression in one compiler
    By jimmyjamesii in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2005, 12:47 AM
  5. Compiler Error: Unsigned vs Signed Integer Expression
    By ChristianTool in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2005, 04:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21