Thread: what is the output of this code?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    what is the output of this code?

    what is the output of the below program?

    Code:
    int main()
     {
       int val=5;
       printf("%d %d %d %d",val,--val,++val,val--);
       return(0);
     }
     (a)3 4 6 5
     (b)Compiler Dependant
     (c)4 4 5 5
     (d)None of these

  2. #2

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    well, compile it...
    Arduino rocks!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    I know that if I compile I will get the answer. But My doubt is that as there is an option of compiler dependent, I am asking you guys if a question like this appears in any exam...which is the correct option??

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I'll give you a hint:

    ++var will increment it before the value is read
    var++ will increment after the value is read

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The rule is that no variable or memory location can be modified more than once in the same expression, except when the use is separated by a sequencing operator. Further, it cannot be accessed and modified in the same expression, unless the value accessed is used to compute the new value, also excepting when the use is separated by a sequencing operator.
    Last edited by King Mir; 09-16-2009 at 06:39 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by karthik537 View Post
    what is the output of the below program?

    Code:
    int main()
     {
       int val=5;
       printf("%d %d %d %d",val,--val,++val,val--);
       return(0);
     }
     (a)3 4 6 5
     (b)Compiler Dependant
     (c)4 4 5 5
     (d)None of these
    The arguments of printf are pushed into the stack in the following two ways:
    1. Right to left
    2. Left to right
    It is entirely dependent on compiler which one to use, thus the answer will be compiler dependent.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Show this to anyone who thinks the answer is anything other than d)
    Expressions
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Salem
    Show this to anyone who thinks the answer is anything other than d)
    I think you should read the options more carefully
    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

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why? None of the options said "undefined behaviour".
    That is NOT the same as compiler specific.
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Salem
    Why? None of the options said "undefined behaviour".
    That is NOT the same as compiler specific.
    "None of these" must be wrong, since due to undefined behaviour "3 4 6 5" or "4 4 5 5" could be the output of the program. Consequently, I reason that "Compiler Dependant" is the best answer, or else one should add another option.

    EDIT:
    On second thought... "None of these" should be admissible, since the output could indeed be neither "3 4 6 5" nor "4 4 5 5". But then you implied that the other answers were wrong, which is wrong.
    Last edited by laserlight; 09-16-2009 at 10:34 AM.
    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

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think compiler specific is meant here is the combined category of implementation defined, and undefined. It's a reasonable use, since the behavior of undefined C code does depend on how the compiler handles it.

    Also, from a realistic point of view, no compiler will do anything besides print some numbers and possibly change the value of val.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's undefined behaviour!

    Even a drunk occasionally manages to stumble into their own house, but that just makes them lucky, not sober.

    Likewise, producing one of the given answers doesn't prove a damn thing about the correctness of the answers. Reading the C standard tells you the answer, not reverse engineering a hypothesis based on observations of a single compiler.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Salem
    Likewise, producing one of the given answers doesn't prove a damn thing about the correctness of the answers. Reading the C standard tells you the answer, not reverse engineering a hypothesis based on observations of a single compiler.
    In other words, if you really want to be correct, then the selection of possible answers is incomplete. Saying "Show this to anyone who thinks the answer is anything other than d)" is bogus, since you should show it to someone who picks d) as the answer. Either that, or you agree that b) is the best answer, even though the other answers are correct.
    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

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is nothing incomplete about the answers at all (logically). Option d is a catch-all else to the other 3 conditions.

    Misleading and unhelpful perhaps, because there isn't a specific option of "undefined behaviour" (which would have been good). If you know the answer, don't see your answer, but do see "none of the above", what else are you supposed to choose?

    The danger of course is that someone who tries the code, and gets an answer which isn't a or c then has a problem - is it compiler specific, or something else?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM