ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
<< 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;
}
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'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 .
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.