Thread: expression evaluation

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    expression evaluation

    Hi ,

    In below code i want to the evaluation of expression.

    I am trying to know like , when will x be incremented and y will be incremented and on what basis.


    Code:
    #include <stdio.h>
    
    int main()
    {
            int x=20,y=35;
            x=y++ + x++;
            y=++y + ++x;
            printf("%d %d\n",x,y);
            return 0;
    }
    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I am trying to know like , when will x be incremented and y will be incremented and on what basis.
    That code is undefined.
    There is nothing to know, except to know that is not how you use those operators.

    Expressions
    and
    Comma Operator - C And C++ | Dream.In.Code
    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.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Well, you can't know because lines 6 and 7 are undefined behaviour (UB). There are no sequence points and the compiler is free to evaluate them in whatever order it likes. See section 6.5 of the C Standard. Nobody would write code like you've provided in real life and not just because it's UB: more importantly it's unreadable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic evaluation of an expression
    By jaymax in forum General Discussions
    Replies: 4
    Last Post: 04-04-2014, 01:26 PM
  2. Evaluation of postfix expression
    By Prateekr10 in forum C Programming
    Replies: 3
    Last Post: 12-29-2009, 09:50 AM
  3. order of evaluation of C-expression
    By Pranav kant in forum C Programming
    Replies: 1
    Last Post: 11-22-2009, 08:24 AM
  4. expression evaluation
    By lazy_hack in forum C Programming
    Replies: 11
    Last Post: 08-21-2009, 09:37 AM
  5. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM

Tags for this Thread