Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mine's pwnsomeness doubts in programming.(maybe ideas too)
#1
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?...
Reply
Thanks given by:
#2
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.
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by:
#3
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.
Reply
Thanks given by: MnM
#4
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.
Reply
Thanks given by:
#5
(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).
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by:
#6
(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.

Reply
Thanks given by: A-Man
#7
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)
Reply
Thanks given by: blow_fly98
#8
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.
4ye 6anDy (Click to View)
We're watching you... (Click to View)
| Avatar made by Alectric |
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)