Thread: unary increment operator execution

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    unary increment operator execution

    Can Somebody please explain how the output for the program below comes to 45 and 46 respectively.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int x=10,y=10;
    clrscr();
    //printf("%d\n",x+(++x)+x+(x++)+x);
    //printf("%d",(x++)+(++x)+x);
    
    
    x=(++x)+(x++)+x+x;
    printf("%d\n%d\n",x,(++y)+(y++)+y+y);
    //printf("value of x is %d",x+(++x)+x+(++x));
    getch();
    }
    
    the output obtained for the above program is 45 and 46.

    Thanks in Advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The behaviour is undefined: C FAQ on expressions

    In other words, instead of asking how the output came to be that, just don't do it. If you must figure it out, have the compiler output assembly instead and check the assembly output. Alternatively, just come up with some sequence of increments that happens to correspond to the output that you see.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well, I dunno. Seems quite readable to me

    Edit: Apart from undefined behaviour, can you think of a single reason (just one) why anybody would write code like that? Is this a competition to see who can write the most absurd, incorrect and stupid C code possible?
    Last edited by Hodor; 11-28-2013 at 04:58 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The code is broken, for the reasons already stated.

    I get different answers.
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main()
    {
      int x=10,y=10;
      x=(++x)+(x++)+x+x;
      printf("%d\n%d\n",x,(++y)+(y++)+y+y);
      return 0;
    }
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:5:4: warning: operation on ‘x’ may be undefined [-Wsequence-point]
    foo.c:5:4: warning: operation on ‘x’ may be undefined [-Wsequence-point]
    foo.c:6:24: warning: operation on ‘y’ may be undefined [-Wsequence-point]
    foo.c:6:24: warning: operation on ‘y’ may be undefined [-Wsequence-point]
    foo.c:6:24: warning: operation on ‘y’ may be undefined [-Wsequence-point]
    $ ./a.out 
    45
    44
    For a more in-depth analysis (more compilers, simpler program), read this thread.
    Post and Pre Increment
    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.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Thank you for the timely reply .....it was informative....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ! unary operator
    By rakeshkool27 in forum C Programming
    Replies: 9
    Last Post: 04-10-2010, 11:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. + unary operator
    By SourceCode in forum C Programming
    Replies: 0
    Last Post: 01-28-2005, 03:52 PM
  4. Overloaded Unary Operator ++
    By Jaymes76 in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2003, 10:36 PM
  5. Unary + operator
    By yeskaran in forum C Programming
    Replies: 4
    Last Post: 07-08-2002, 10:27 AM

Tags for this Thread