Thread: What is the output & please explain?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    What is the output & please explain?

    Code:
    #include<stdio.h>
    int main()
    {
        int a=2,b=3;
        a=a + b - (b=a);
        printf("%d %d",a,b);
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ackr1201
    What is the output & please explain?
    You can find out the output yourself by compiling and running the program. I would have expected you to have done that first, found that something was amiss, and then ask why.

    As for why: the answer is related to the explanation given for the question in the recent thread post and pre increment operator
    . In short, you are observing the effects of undefined behaviour because b is modified when it is also accessed for its value other than for the modification.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Who teaches this rubbish!?
    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.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    @laserlight: Thx for the suggestion....

    My question is that why the output behavior is undefined?

    I am thinking that parenthesis is executed first always(as it has highest priority). If it is so then it will give unique answer on all compilers but in reality its not happening(as i know answer is undefined). This is my confusion.
    plz help me!!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Who teaches this rubbish!?
    Exactly... and how do we get him to stop publishing the URL for these forums?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ackr1201 View Post
    @laserlight: Thx for the suggestion....

    My question is that why the output behavior is undefined?

    I am thinking that parenthesis is executed first always(as it has highest priority). If it is so then it will give unique answer on all compilers but in reality its not happening(as i know answer is undefined). This is my confusion.
    plz help me!!
    In super noobish terms (even though the answer is somewhat more complicated)... because the value of b is being changed twice in a single expression.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Exactly... and how do we get him to stop publishing the URL for these forums?
    No, the so-called teachers over there NEED to come and visit, so they can actually get a freaking clue about what they're trying to teach.
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > Exactly... and how do we get him to stop publishing the URL for these forums?
    No, the so-called teachers over there NEED to come and visit, so they can actually get a freaking clue about what they're trying to teach.
    Well, that's one way to go....

    Frankly, I could do without the hassles...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain why output is 4??
    By ackr1201 in forum C Programming
    Replies: 6
    Last Post: 07-17-2011, 03:20 AM
  2. explain the output ?????
    By shashwat in forum C Programming
    Replies: 4
    Last Post: 07-09-2011, 06:32 AM
  3. 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
  4. help: please explain the output
    By soniclavier in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 12:35 PM
  5. plz explain the output of the program.
    By raj_ksrt in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 03:43 AM