Little Fighter Empire - Forums
Mine's pwnsomeness doubts in programming.(maybe ideas too) - Printable Version

+- Little Fighter Empire - Forums (https://lf-empire.de/forum)
+-- Forum: General Zone (https://lf-empire.de/forum/forumdisplay.php?fid=23)
+--- Forum: Programming (https://lf-empire.de/forum/forumdisplay.php?fid=50)
+--- Thread: Mine's pwnsomeness doubts in programming.(maybe ideas too) (/showthread.php?tid=7898)



Mine's pwnsomeness doubts in programming.(maybe ideas too) - MnM - 06-04-2012

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?...


RE: postfix and prefix operators. - A-Man - 06-04-2012

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.



RE: postfix and prefix operators. - Reaper - 06-04-2012

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.


RE: postfix and prefix operators. - Som1Lse - 06-04-2012

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;
}



RE: postfix and prefix operators. - A-Man - 06-04-2012

(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).


RE: postfix and prefix operators. - Reaper - 06-04-2012

(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.




RE: postfix and prefix operators. - MnM - 06-06-2012

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:
[Image: Cvvhw.png]


Reply in-thread (Click to View)



RE: postfix and prefix operators. - blow_fly98 - 06-07-2012

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.