-2

I am still trying to get my way into C++ and I've written this code:

#include <iostream>
using namespace std;

int main()
{

    cout << " "<< endl << cout << "Hello world!" <<endl;

}

The output is:

1Hello world!

Why is there a 1 before the Hello World?

Edit: My program does compile, it seems that I have an old compiler version.

3 Answers3

7

The short answer to your question is that the syntax you're using to output data is slightly off. If you chain together a bunch of output statements, the convention is to put the stream at the far left and to not repeat it. So rather than writing

cout << " " << endl << cout << "Hello world!" << endl;
                       ~~~~~~~

just write

cout << " "<< endl << "Hello world!" << endl;

The reason you're seeing a 1 here is somewhat technical. The stream types all provide an overloaded operator that you can use to test for whether the stream is valid. For example, you can write something like this:

if (cout) {
    // Everything is okay!
} else {
    // I don't know how you did it, but you broke cout and you can
    // no longer write anything to it!
}

(This is mostly commonly used for input streams, but output streams support this as well). As a consequence of this syntax, if you try inserting cout into an output stream, C++ will first try to convert cout to a boolean value and print that value instead. By default, booleans get printed as 1 (true) or 0 (false), so the 1 you're seeing is C++ saying "yes, this stream is up and running."

(Technically speaking the overloaded operator produces a void* rather than a bool, but I'll gloss over that detail for now.)

As a note, this behavior isn't supported in modern versions of C++ (C++11 and forward), and you'd actually get a compiler error if you tried doing this with a modern compiler. If possible, I would recommend upgrading your compiler version, which would have given you an error rather than generating code that doesn't do what you think it does.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • I think this would fall under semantics more than syntax, although bang on besides that, what compiler is used to get this to run, it would only throw errors for me – Nick is tired Dec 19 '17 at 18:16
  • Will it generate a compiler error by using a modern compiler alone, or only by actually enabling C++11? (which is still not the default in many cases afaik) – mkrieger1 Dec 19 '17 at 18:23
  • 1
    @mkrieger1 That's a good point - you'd probably need to enable C++11 (or C++14 or C++17) mode for this. – templatetypedef Dec 19 '17 at 18:26
  • @mkrieger1 Newer GCC and trunk Clang default to -std=gnu++14 ("C++14 plus non-standard-conforming GNU extensions" mode). VS2017 does not have C++98 or C++11 mode at all. – cpplearner Dec 20 '17 at 12:52
1

In my lapi and I'am using CodeBlocks and getting output as

0x489944Hello world!

It's happening because cout is an object ostream class and when you are doing something like

cout << " "<< endl << cout << "Hello world!" <<endl;

first cout is printing on the console screen and second cout is being treated as value to be printed along with the "hello world" which is the value to be printed by second cout.

So basically you are getting output "Hello world" from the second cout and you are getting 1 or some other numeric value before hello which is being printed by the 1st cout as the reference address of 2nd cout.

In your case 1 is being printed on the console as the reference address of cout which may vary compiler to compiler.

enter image description here

Gaurav Kumar
  • 334
  • 1
  • 7
  • 1
    I believe the 1 comes from a boolean conversion – Basile Starynkevitch Dec 19 '17 at 18:24
  • 1
    It looks like in C++03, `cout` had `operator void*() const;` that essentially did `return fail() ? NULL : this;`. C++11 moved to `explicit operator bool() const;`. 1 is not "*the reference address of `cout`*," you're just not compiling with C++11 or later. – scohe001 Dec 19 '17 at 18:31
0

For most << operations, cout << x (where x is of "most" type) returns cout itself.

(this is not an absolute rule, you could define some operator << where it is not true; but it is usually the case)

So cout << " "<< endl << cout is parsed as ((cout << " ") << endl) << cout

So is the same as:

auto o1 = cout << " ";
auto o2 = o1 << endl;
auto o3 = o2 << cout;

So the first assignment (of o1) outputs a space and return cout (actually, a reference to it).

The second assignment of (of o2) output an end of line, flush the buffer, and return cout.

The assignment of o3 computes cout << cout;

There is no defined overloading of that operator. The right cout is converted to a bool, and the net effect is the same as cout << true which outputs 1 and returns cout

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547