-5

So I just started teaching myself C++ and I have two newbie questions regarding the Hello World exercise.

#include <iostream>
using namespace std;                    [1]

int main()
{
    cout << "Hello, World" << endl;     [2]
    return 0;
}

[1] Is this line of code necessary? If not, why? It worked without it but I found a source that used it and was wondering why was this used.

[2] On my first try I forgot to add endl and the code worked. When I went to check I realised this was missing so why did it still work anyway?

Really basic questions but I want to understand the basics well.

Many thanks in advance.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
bakashinji
  • 135
  • 2
  • 11
  • _One_ question per question. Not two. The clue's in the name. Question #1 is trivially found on Stack Overflow already, many times over. I suggest sticking with #2. – Lightness Races in Orbit Jul 26 '16 at 21:16
  • 3
    _"It worked without it"_ Unlikely.. – Lightness Races in Orbit Jul 26 '16 at 21:17
  • `endl` = end line, e.g. a line break. the world's not going to end because a program didn't output a line break after saying hello world. – Marc B Jul 26 '16 at 21:18
  • Have you by any chance been using StackOverflow Documentation to teach yourself C++? – TerraPass Jul 26 '16 at 21:20
  • [1] It didn't work without it. Nevertheless, don't use it, write `std::cout` instead of `cout` (and `std::endl` instead of `endl`, or even better, just write `\n`). [2] If you leave out the comma in "Hello, world" the program also compiles! – CompuChip Jul 26 '16 at 21:21
  • Thanks for the help and patience :) – bakashinji Jul 26 '16 at 21:22
  • you may want to do a more thorough google search next time. The answers to your questions may be found [here](http://en.cppreference.com/w/cpp/language/namespace) and [here](http://stackoverflow.com/questions/213907/c-stdendl-vs-n) – jaggedSpire Jul 26 '16 at 21:25
  • @CompuChip While your point about [not `using namespace std;`](http://stackoverflow.com/q/1452721/5044950) is valid, the rest of your comment is opinion-based and should not be presented as fact; for instance, `using std::cout;` and `using std::endl;` would also work. Also, while one could argue that `\n` should *generally* be used instead of `std::endl`, there's no real reason that the former is objectively "even better" in this case. – Sam Estep Jul 26 '16 at 21:29

1 Answers1

3

Is this line of code necessary? If not, why? It worked without it but I found a source that used it and was wondering why was this used.

Namespace

First of all you should have to understand what a namespace is. That's an argument reference: Namespace.

Pratically a namespace is like a container. You can keep different symbol's names. In that way, in very large project, it is possible define two different symbols (e.g two functions) with the same name.

I try to give you a little example: I can define two different functions foo with the same name. It possibile because I put them inside two different namespaces.

namespace my_ns1 {
  void foo(int a) {
    return a;
  }
}

namespace my_ns2 {
  void foo(int a) {
    return a + 2;
  }
}

When I want to call the first foo function the proper invokation will be:

my_ns1::foo(10);    // return 10

If i want to call the second foo function, then:

my_ns2::foo(10);    // return 12

In a specific block I can specify the intent to use always a namespace with the code:

using namespace my_ns1;

In that way there is no more need to specify the "full name" of the function.

The standard library keeps all its function in a proper namespace: std. So when you want to use a function in the standard library you have to invoke it with something like:

std::function(...)

If you use the code

using namespace std;

At the begin of your file, you're just saying to "open" that namespace and you can call all function without std::

The namespace is usefull in order to prevent name conflict.


[2] On my first try I forgot to add endl and the code worked. When I went to check I realised this was missing so why did it still work anyway?

Simply

std::endl

is a proper way to insert the '\n' character which means "an end of line".

BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • 3
    `std::endl` not only put's `\n` into the stream, it also flushes the stream. Which is a very important notice a lot of people seem to skip. – adnan_e Jul 26 '16 at 21:40
  • 3
    Also, you should mention that it's [discouraged to use `using namespace`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). If you really want to use it, then use it like `using std::cin` – adnan_e Jul 26 '16 at 21:44
  • Small correction: `endl` is more than just a `'\n'`. It is a `'\n'` and an instruction to "flush" the output stream, to write it to whatever underlying media the stream represents. This flush can be brutally expensive, so it's in your best interests to not do it unless you have to. What you typically want to do with a stream is to write to it, allow it to fill up its buffering space, and only when that buffer is full, or you must commit the buffered data, write the data. – user4581301 Jul 26 '16 at 23:48
  • Yeah I know and thanks for your punctualization. I missed that just 'cause the level of the post is very "basic" and i've found useless to add this information without a good explanation of "flush" "buffer" and "stream". – BiagioF Jul 27 '16 at 04:49