Posts: 1,229
Threads: 54
Joined: Sep 2008
06-04-2012, 12:20 PM
(This post was last modified: 06-07-2012, 01:51 PM by MnM.)
I recently saw an interesting program in my text book.
Code: #include<iostream.h>
#include<conio.h>
void main()
{
int a;
a=5;
clrscr();
cout<<a<<a++<<++a;
}
Guessed output : 557
Output witnessed : 766
can anyone explain it with using Stack concept or something?...
Thanks given by:
Posts: 1,556
Threads: 77
Joined: May 2011
06-04-2012, 02:26 PM
(This post was last modified: 06-05-2012, 12:01 AM by A-Man.)
As we all know, the ++ operator as the prefix, evaluate before processing (printing in this case), while the ++ operator as a suffix will process then evaluate.
so the code is now like:
cout<<7(a is 7)<<6(+1, but later after the printing the result. After the print, a = 7)<<6(5+1, evaluated at that moment and a is printed and stored as 6);
Note: remember the code is processed from left.
Thanks given by:
Posts: 813
Threads: 17
Joined: Mar 2009
06-04-2012, 02:54 PM
(This post was last modified: 06-04-2012, 03:14 PM by Reaper.)
Why would anybody ever want to write such code?
I think the best idea is to just avoid it. If I run that programm (with slight changes because my compiler doesn't let me use void main or conio.h), I get 767 as a result. I think it depends on the compiler when the pre- and postfix-operators are applied.
Technically it's (at least in C++) undefined behaviour because you cannot modify a variable more than once between sequence points (semicolons in this case.). Also see >here<.
Edit: Java(eclipse) has 557 as output :P I think Java has a standard way of working with stuff like that, though.
Posts: 746
Threads: 55
Joined: Apr 2008
06-04-2012, 04:21 PM
(This post was last modified: 03-03-2015, 05:10 PM by Som1Lse.)
Like reaper said, it is compiler-related and should be avoided.
Code that works like it should:
C++-Code:
#include<iostream.h>
#include<conio.h>
void main(){
int a;
a=5;
clrscr();
cout<<a;
cout<<a++
cout<<++a;
}
|
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
Trump is an idiot.
Thanks given by:
Posts: 1,556
Threads: 77
Joined: May 2011
(06-04-2012, 02:54 PM)Reaper Wrote: Why would anybody ever want to write such code?
I think the best idea is to just avoid it. If I run that programm (with slight changes because my compiler doesn't let me use void main or conio.h), I get 767 as a result. 767????? I can't think of anyway the computer can process that to give that output unless it starts from middle, right then left, and thats doesn't make sense at all. I guess that example shows the output using the text-book' compiler.
@M.M: I think this example was just given in the book to show how the postfix and prefix work. Its nice to understand how every thing works carefully, but don't make that thing confuse you (me: talking as an expert, lol).
Thanks given by:
Posts: 813
Threads: 17
Joined: Mar 2009
(06-04-2012, 04:26 PM)A-MAN Wrote: 767????? I can't think of anyway the computer can process that to give that output unless it starts from middle, right then left, and thats doesn't make sense at all. What I'd guess is that my compiler does the printing after it looked through the whole line/tries to do the printing as late as possible. So it's like: Alright, I can increment by 1 here when he's at the last a. Means a is now 6. Also I've got another ++, so a is 7, but I have to process the middle expression first, so I've got to put 767 out.
Just a speculation, though.
Posts: 1,229
Threads: 54
Joined: Sep 2008
I've got a solution for it. You must have guessed that by the use of "<<"(extraction operator) that i worked with c++. Also, it works really fine for me when i experimented with stack concept. (btw, i am using Turbo C++).
Explanation:
(06-04-2012, 02:54 PM)Reaper Wrote: Edit: Java(eclipse) has 557 as output :P I think Java has a standard way of working with stuff like that, though.
Yes, java's execution style is different and also the stuffs are more clear in print statement is concerned.
Posts: 410
Threads: 25
Joined: Sep 2008
Interesting compiler-related topics. I thought that stack-related concepts would be invariant among compilers (at least for C++ compilers) but the information in this thread seems to point otherwise.
Another thing I found interesting: Variables shouldn't be assigned values more than once between code points. Guess I shouldn't be lazy anymore, huh.
Hiding users' signatures and avatars was the best decision ever.
hey blow :P ~ Bamboori
can I call ya koalafly? ~ Phil
Pogofly - Simoneon
k0a1af1y, looks much more stylish ~ Blue Phoenix
So hey, blow, say something! - Simoneon
Quack? ~ blow_fly98
Hi blow ~Ramond
So... is it decided that we call him "blow"? ~ Blue Phoenix
No. ~ blow_fly98
But blow, all the members now will follow us because of this convo. You're ruined!!! - Simoneon
Meh. But I'll leave this convo here anyway. ~ blow_fly98
meh koala is much more awesome so I will stick to that ~ Phil
Wait, what? Koalas can edit profiles? - Simoneon
lol, seems that i started a long discussion :P ~ Bamboori
You did, indeed. I wonder who will exceed the sig-limit... ~ Blue Phoenix
Please don't trash this. It's good eye-candy. ~ blow_fly98
koala has spoken and so shall it be =) ~ Phil
there're some html here, and bla bla bla, so I think I might exceed it. Period - Simoneon
WHY THE SPOILER?! ~ Blue Phoenix
didn't you spoiler the conversation yourself? ~ Bamboori
BLOW BLOW!!! YOU FORGOT TO WRITE NAKATO IN 'WE'RE WATCHING YOU'!!!!! - Simoneon
To0 8ad. H4 D045n't d0 any MoDdin9 at A11!!!!!!! ~ blow_fly98
Evil Sonidow does, eh? - Simoneon
Of course. Just n0t v4ry 0ft4n. ~ blow_fly98
WHY THE 1337-SPEAK?! ~ Blue Phoenix
Because I am a KOALA!!! ~ blow_fly98
To be honest, I thought they use Eucalyptus-Speal..........
......
......
D: - Simoneon
Noez, Simon forgot to close his notice! ~ Blue Phoenix
oh no its the end of the world! *panic* ~ Bamboori
Gzz Hizz Fz Mzzz
Anyone got a Eucalyptus-speak interpreter?
And did you know that a user can have more than 4 images in their signature if we edit it for them? Or am I just the last one to find out?
~ blow_fly98
OMG spoilers in spoilers! ~ Bamboori
OMG offtopic in offtopic! - Simoneon
OH HAI GUYZ, i didnt you see you thar :P ~ Alectric
Now, I seriously need to be a part of this. ~ SirFrog
Wasn't there something about childish mods editing signatures lately? ~ Reaper
What?!?! Where was it?!?!?! ~ blow_fly98
DO YOU HAVE SUGAR?! - Simoneon
I heard if you edit someone's signature enough, your profile automatically is set to Admin status. BP told me. ~ Alectric
then let's do this! ~ Bamboori
Just because I don't go around checking every single post and editing signatures doesn't mean that I don't mod here. I just don't check posts that aren't reported. ~ Evil Sonidow
Seriously. We should encourage people to start reporting more :/ - Simoneon
EvErYoNe'S pOsTeD hErE eXcEpT mH.
Hey, maybe we could report every single post just so ES could check them. ~ blow_fly98
You know, that's a hell lot of work. What about a rule that everyone has to report at least one post whenever he logs in? Or an auto-report function? ~ Reaper
I liked the report-part until we got to the point of an auto-report function. That's something that requires work --> No. ~ Blue Phoenix
Finally, I get to see some invisible users. *sniggers* ~ blow_fly98
Oh, i came back. Well, since im already here, i may as well shoot bees ~ Alectric
You know, the ducks are watching. Always watching... ~ SirFrog
Hmm, I think, Lauli is about to climax due to the awesomeness of this color-wall ~ Blue Phoenix
<table border="0" cellspacing="1" cellpadding="4" class="tborder">
<thead>
<tr>
<td class="thead">
<div class="expcolimage"><img src="images/unrealblack//collapse.gif" id="boardstats_img" class="expander" alt="[-]" title="[-]" /></div>
<div><strong>Board Statistics</strong></div>
</td>
</tr>
</thead>
<tbody style="" id="boardstats_e">
<tr>
<td class="tcat"><span class="smalltext"><strong>Who's Online</strong> </span></td>
</tr>
<tr>
<td class="trow1"><span class="smalltext">13 users active in the past 5 minutes (13 members, 0 of whom are invisible, and 0 guests).<br /><a href="member.php?action=profile&uid=838"><span style="color: #ff7f00"><b>Alectric</b></span></a>, <a href="member.php?action=profile&uid=17"><span style="color: #ff7f00"><b>Azriel</b></span></a>, <a href="member.php?action=profile&uid=26"><span style="color: #8888ff"><b>Bamboori</b></span></a>, <a href="member.php?action=profile&uid=1056"><span style="color: #008800"><b>blow_fly98</b></span></a>, <a href="member.php?action=profile&uid=15"><span style="color: #ff0000;"><b>Blue Phoenix</b></span></a>, <a href="member.php?action=profile&uid=5"><span style="color: #8888ff;"><b>Evil Sonidow</b></span></a>, <a href="http://www.lf-empire.de/forum/member.php?action=profile&uid=8"><span style="color: #8888ff;"><b>Lord Silva</b></span></a>, <a href="member.php?action=profile&uid=3"><span style="color: #8888ff;"><b>Marshall</b></span></a>, <a href="member.php?action=profile&uid=1"><span style="color: #ff0000;"><b>MH-Razen</b></span></a>, <a href="member.php?action=profile&uid=736"><span style="color: #8888ff;"><b>Phil</b></span></a>, <a href="member.php?action=profile&uid=10"><span style="color: #ff7f00;"><b>Ramond</b></span></a>, <a href="member.php?action=profile&uid=1659"><span style="color: #008800"><b>Reaper</b></span></a>, <a href="member.php?action=profile&uid=1317"><span style="color: #ff7f00;"><b>Simoneon</b></span></a>, <a href="member.php?action=profile&uid=245"><span style="color: #008800"><b>SirFrog</b></span></a></span></td>
</tr>
</tbody>
</table> | Avatar made by Alectric |
Thanks given by:
|