+ 1

Arithmetic cout like cout << 2+2; prints 4 in Visual Studio, but does not print anything in gcc compiler. Why ?

In C++, cout for basic arithmetic operations gives out result directly, but it is not in GCC 5.4.0.

26th Feb 2018, 8:34 AM
Harshit
9 ответов
26th Feb 2018, 9:21 AM
jay
jay - avatar
+ 6
From the observations posted here, I think it has to do with how the compiler handles the operator precedence. It most probably evaluated (cout << 2) and then added 2 to the cout object. (But this should not have compiled properly... ) Observe what happens when you do: cout << cout; I might need you to try what happens when you do: cout << (cout << 2 + 2);
26th Feb 2018, 9:08 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
I just tried to run cout << 2+2; on an online compiler with GCC 5.4.0, and it gave me 4, so I'm not sure what is causing the issue on your side.
26th Feb 2018, 9:18 AM
Hatsy Rei
Hatsy Rei - avatar
26th Feb 2018, 9:18 AM
Timon Paßlick
+ 1
This was due to our connection. Now it outputs 4.
26th Feb 2018, 9:20 AM
Timon Paßlick
0
With () braces, it works. Strange...
26th Feb 2018, 9:04 AM
Timon Paßlick
0
Nope, still with () too.
26th Feb 2018, 9:07 AM
Harshit
0
It is not showing anything.
26th Feb 2018, 9:10 AM
Harshit
0
In SL, there is no output.
26th Feb 2018, 9:19 AM
Timon Paßlick