Discuss Simple c program-please explain output. in the C Programming forum on Dev Shed. Simple c program-please explain output. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 4,807
Time spent in forums: 1 Month 2 Days 17 h 20 m 11 sec
Reputation Power: 1800
Quote:
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.
Posts: 2,476
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 42 m 18 sec
Reputation Power: 2194
Quote:
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!
"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
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 29 m 56 sec
Reputation Power: 4080
Quote:
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 . 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
Posts: 3,357
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
Since python hasn't got ++ -- operators,
the python result is strictly determined by precedence and the result of monadic negation and monadic identity.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 29 m 56 sec
Reputation Power: 4080
Quote:
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.
Posts: 1,936
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
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.)
Posts: 4,807
Time spent in forums: 1 Month 2 Days 17 h 20 m 11 sec
Reputation Power: 1800
Quote:
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.