Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Exclamation Simple c program-please explain output.


    Code:
    int main()
    {
         int a=7, t=0;
         t=--a+--a+a+++a;
         printf("%d",t);
    }
    output: 20

    I could not understand why it prints '20' ? please explain it !
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by hitesh.incept
    I could not understand why it prints ? please explain it !
    Because it is bad code - a counter-example to illustrate something you should avoid. It is common to have these puzzles set as academic exercises, but they are pointless - what do you learn from this? - That implausible code is hard to fathom and unlikely to work - so what!?

    An example of dangerous code that a reasonable but unwary person might nonetheless write might be useful, but no reasonable person would write code like that, so you learn nothing useful by doing this because you would not reasonably write such code if your intent was to get some work done!

    With 22 years of professional software development I cannot answer your question, mostly because I choose not to wast my time of such exercises; not knowing that has not hindered my career - in fact while you think about it, I'll get on with something useful and productive.

    If your tutor or professor set their exercise, tell him to stop wasting your time (and ours), and teach you something useful.

    Comments on this post

    • Shaun_B agrees : (y)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Smile


    thanks to b49P23TIvg, clifford :)
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,556
    Rep Power
    2338
    Because it is bad code - a counter-example to illustrate something you should avoid
    I wish I could be convinced that is the purpose of this stupid assignment, but somehow I doubt it is. We are talking about the land of Turbo C after all.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by b49P23TIvg
    This example has the additional ambiguity of operator identification. for example is:
    Code:
    a+++a
    interpreted as
    Code:
    a + ++a
    or
    Code:
    a++ + a
    ?

    I still don't know and still don't care!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    I'd guess that's not ambiguous. Like you, I didn't investigate.

    Comments on this post

    • clifford agrees : It is ambiguous to a human even if the compiler interpretes it in a defined manner. If I don't know what that defined behaviour is it remains ambiguous to me.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    If anyone else is interested, PHP gives 22:

    Code:
    php > $a = 7; echo --$a+--$a+$a+++$a;
    22
    Python gives 28:

    Code:
    >>> print --a+--a+a+++a;
    28
    Perl gives 21:

    Code:
    :~$ perl -e 'my $a = 7; print --$a+--$a+$a+++$a;'
    21
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Originally Posted by ManiacDan
    If anyone else is interested, PHP gives 22

    Python gives 28:

    Perl gives 21
    Yes, but who knows what it'll say when the next version of PHP (or perl or python) is released :p. Might be interesting to see what happens if you use IronPython or JPython or facebook's custom PHP compiler.

    The bad thing about expressions like this is that the same C compiler may give you different values of output depending on the optimization settings. All the answers are correct per the standard, as the result is undefined.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Since python hasn't got ++ -- operators,
    the python result is strictly determined by precedence and the result of monadic negation and monadic identity.

    >>> print --a+--a+a+++a
    28

    is the same as 4 * a
    and therefore a was 7.
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Originally Posted by b49P23TIvg
    Since python hasn't got ++ -- operators,
    the python result is strictly determined by precedence and the result of monadic negation and monadic identity.
    Good point :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by b49P23TIvg
    Since python hasn't got ++ -- operators,
    the python result is strictly determined by precedence and the result of monadic negation and monadic identity.

    >>> print --a+--a+a+++a
    28

    is the same as 4 * a
    and therefore a was 7.
    More evidence in favor of Python being the sanest modern programming language. (I'm not saying it definitely is, but I'd wager it's in the top three.)
  24. #13
  25. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Lux Perpetua
    More evidence in favor of Python being the sanest modern programming language. (I'm not saying it definitely is, but I'd wager it's in the top three.)
    The problem is that if you post Python code here without code tags it becomes semantically different by loss of indentation and impossible to reverse back to the original (short of viewing the raw HTML). For that reason alone it is insane. ;) On the other hand you never get any arguments about the one true style -but that speaks more to the insanity of coders rather than the sanity of Python.
  26. #14
  27. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by Lux Perpetua
    More evidence in favor of Python being the sanest modern programming language. (I'm not saying it definitely is, but I'd wager it's in the top three.)
    Wait...lack of the -- and ++ operators makes it more sane?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  28. #15
  29. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo