LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2004, 10:21 AM   #1
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Strange Output (C++)ْ


Hi,
I don't know why the output of the following program is
Code:
first: 3, second: 2, third: 1
first: 6, second: 5, third: 4
the second line is reasonable to some extent (because of the way, C puts the arguments into stack - which is right to left), but what about cout? << is an operator and should be evaluated left to right?!

Code:
#include <iostream>
#include <stdio.h>

using namespace std;

int f() {
	static int i = 1;
	return i++;
}

int main () {
	cout << "first: " << f() << ", second: " << f() << ", third: " << f() << endl;
	printf ("first: %d, second: %d, third: %d", f(), f(), f());
	getchar ();
	return 0;
}

Last edited by Mohsen; 05-06-2004 at 10:26 AM.
 
Old 05-06-2004, 12:00 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
<< is an operator and should be evaluated left to right?!
I looked it up. And you're right about this. It's indeed strange, and I don't understand what is exactly happening. To find out, I tried the code below, but it even confused me more...
(Try it, and see what happens. The second line of output is even more weird!)
Code:
#include <iostream>
#include <stdio.h>

using namespace std;

int f() {
	static int i = 1;
	return i++;
}

int main () {
	 (((cout << f()) << f()) << f());
	 cout << endl;
	 cout << (f() << (f() << (f())));
	 cout << endl;
	 return 0;
}
Anybody an idea?

Last edited by Hko; 05-06-2004 at 12:02 PM.
 
Old 05-06-2004, 12:59 PM   #3
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
Quote:
cout << "first: " << f() << ", second: " << f() << ", third: " << f() << endl;
if you take that statement a break it into three like so
Code:
cout << "first: " << f();
cout << ",second: " << f();
cout << ",third: " << f() << endl;
you get the expected output first: 1, second: 2, third: 3
I would suspect the single cout call processes it's arguments on a stack similarly to printf.

I don't know for sure but that's my best guess.
 
Old 05-07-2004, 02:28 AM   #4
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Original Poster
Rep: Reputation: 30
I've found the answer!
<< is evaluated like =. See the code below:
Code:
int a = 1, b = 2, c = 3;
a = b = c;
The compiler first put c into b and after that the newer b into a. likewise
Code:
int i = 0;
cout << ++i << i++;
the output will be `20', because of the sequence of evaluating some same operators. It will change it to cout << 2 << 0 first, and then put it on the screen normally (from left to right). Evaluating is right to left as you can see.
After all, it can be a very nice exam question .
 
Old 05-07-2004, 06:45 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Evaluating is right to left as you can see.
That's exactly what I find strange!
Because the book "The C++ Programming Language, special edition" (Bjarne Stroustrup, the creator of C++) says in section 6.2 on page 121:
Quote:
Unary operators and assignment operators are right-associative; all others are left-associative.
Furthermore, in C++, precedence and associativity cannot be changed for overloaded operators.
 
Old 05-07-2004, 02:03 PM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
This isn't about operator precedence.

The problem (simply described) is that you are making three function calls in one statement.

Operator precedence tells the compiler how to use those return values.
c++ places no restriction on the order in which those functions are evaluated.

To determine the order of evaluation you need a "sequence point".
The best definition I could find quickly comes from the devil's own mouth:

http://msdn.microsoft.com/library/de...m/expre_11.asp

HTH

[edit] This is not just about 'function' calls per se ... it applies to any sub-expression, the first example happened to use a function.

Last edited by dakensta; 05-07-2004 at 02:10 PM.
 
Old 05-07-2004, 04:45 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by dakensta
This isn't about operator precedence.
That was clear already. I was assuming associativity controls the order in which side-effect occur.

But never mind, it's clear you got it right. Thanks!

Quote:
The best definition I could find quickly comes from the devil's own mouth:[/B]
That indeed explains what actually is going on. Hard to swallow I had to learn this from a Microsoft source. But credits where credits are due.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange continuous output Nightfrost Linux - General 2 11-02-2004 04:28 AM
Strange traceroute output egurski Linux - Networking 0 07-13-2004 06:25 PM
Strange dmesg output voyciz Linux - Networking 3 06-08-2004 12:05 PM
very strange dmesg output salparadise Linux - Software 6 04-08-2004 11:34 AM
strange ofstream output maarten Programming 2 09-16-2003 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration