Thread: can anyone explain me why output is this ?

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    can anyone explain me why output is this ?

    Code:
    void main(){
    	int a,b;
    	a=b=1;
    	a=a++ + ++b;
    	b=b++ + ++a;
    	printf("%d%d",a,b);
    }
    ouput of program is 5 8
    how?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, it's the same explanation as one of your previous "please explain" threads.

    This is getting really old and tiresome...
    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
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    when i calculate, i found answer 4 6

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As before, the behaviour is undefined on these two lines:
    Code:
    a=a++ + ++b;
    b=b++ + ++a;
    Basically, whenever you see an a++ or ++a and you also see a somewhere else in the expression, it is likely that the behaviour is undefined. There are a few exceptions, perhaps most common is when the subexpressions are separated by && or || because these two operators introduce a sequence point.

    Undefined behaviour means that anything can happen. So, come up with whatever explanation you like, or compile to assembly and check the assembly output.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is the output 4,4,4? pls explain
    By surbhijain93 in forum C Programming
    Replies: 3
    Last Post: 10-04-2014, 02:52 AM
  2. What is the output & please explain?
    By ackr1201 in forum C Programming
    Replies: 7
    Last Post: 10-11-2011, 11:25 PM
  3. Please explain why output is 4??
    By ackr1201 in forum C Programming
    Replies: 6
    Last Post: 07-17-2011, 03:20 AM
  4. explain the output ?????
    By shashwat in forum C Programming
    Replies: 4
    Last Post: 07-09-2011, 06:32 AM
  5. what will be the output and plz explain me
    By alekhya k in forum C Programming
    Replies: 6
    Last Post: 06-22-2011, 09:48 AM