what is the output of this code?

This is a discussion on what is the output of this code? within the C Programming forums, part of the General Programming Boards category; 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); } ...

  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
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    MTK
    MTK is offline
    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,021
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
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-15-2002, 11:04 PM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21