-3

I`ve seen 2 versions of writing the "Hello, world!" and they are slightly different. The question is: which is right?

In VS community machine application :

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}

Siddhartha Rao "С++ 1 hour a day" (2017) :

#include <iostream>

int main ()
{
  std::cout « "Hello World!" « std::endl;
  return 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 5
    The 2nd is definitely wrong. (Typographic `«` is not a valid operator in C++). Beside of this, the `return 0;` is optional in `main()`. ([cppreference.com](https://en.cppreference.com/w/cpp/language/main_function): _4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;._) – Scheff's Cat Mar 14 '22 at 15:52
  • Both of them will seemingly do the same thing, so both of them are right. You won't find the actual differences particularly interesting until you've been studying C++ for, oh, I'd say a year. So, you have plenty of time to learn a bunch of other stuff, first... – Sam Varshavchik Mar 14 '22 at 15:53
  • Another difference is here: [std::endl](https://en.cppreference.com/w/cpp/io/manip/endl). A flush is forced. Useless in most codes. – Damien Mar 14 '22 at 15:54
  • 1
    `std::endl` is almost always unnecessary. You'll see lots of code here that wastes time with it. You don't need the extra work that it does; `'\n'` ends a line. – Pete Becker Mar 14 '22 at 15:57
  • @PeteBecker Some platforms flush when they see `\n` which is why it might seem useless. On my machine, if I use `\n` instead of every `std::endl` I won't see any output until the program finishes. – Offtkp Mar 15 '22 at 10:14
  • @Offtkp -- the behavior you're describing does not conform to the C standard. stdout is ordinarily line-buffered (i.e., flushes on each newline); it can also be fully buffered (which is what you describe), but **only if** "the stream can be determined not to refer to an interactive device". C11, 7.21.3. So using `std::endl` instead of `'\n'` is at best redundant, but when you redirect standard output to a file you'll get horrible performance because of all the unnecessary flushes. – Pete Becker Mar 15 '22 at 13:42
  • @Offtkp -- also, note that I said it's "almost always unnecessary", not that it's "useless". – Pete Becker Mar 15 '22 at 13:48

2 Answers2

2
#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}

This is absolutely correct. The main() function is a special case in that you do not have to return any value from it; if you reach the end of main() without a return a return value of 0 is assumed.

#include <iostream>

int main ()
 {
  std::cout « "Hello World!" « std::endl;
  return 0;
  }

First off, this is badly (inconsistently) indented.

Having a space between the function name and the ( of the parameter list is uncommon, but legal.

It also uses '«' ('\xab') instead of <<, which might be a typo by you -- if it isn't, that is a syntax errror as '«' is not understood by C++.

It is using std::endl instead of \n. This is a more difficult case.

Writing std::endl to an ostream does two things -- write a newline, and flush the output buffer. Whereas writing "\n" is only guaranteed to write the newline; whether it flushes the buffer depends on the buffering policy of the stream.

Output streams can be unbuffered (every byte is written immediately), line buffered (buffer is flushed at every written newline or when full), or fully buffered (buffer is flushed if full or flushed explicitly).

Output streams are line-buffered by default unless the implementation can assert that the output is not an interactive device... which excludes your terminal (which is an interactive device).

Hence, whether you write std::endl or "\n" does not make a difference... except where it does. In this specific case, where you are exiting the program just after the std::cout, it does not make a difference -- any output stream will be flushed before the program exits anyway.

The return 0; is optional, as I said before. It does not hurt having it there, and personally I prefer writing it out just as to not rely on anything "specific" at that point and not to confuse readers who do not know about the implicit return 0 for main(). Your mileage may vary.


Side note on main(): In C, a function declared with empty parameter list could be called with any number of arguments. This is a leftover from the ancient K&R style. If you want to make sure a function can only be called without arguments, in C, you have to declare it with ( void ) as parameter list, i.e. int main( void ). This is recommended practice.

In C++, int main() and int main( void ) are the same thing, and writing out that void is uncommon.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

There is just no need for return 0 for main(), unless you want to end the program before control reaches the end. For example:

#include <iostream>

int main()
{
    int i = 0;

    while (true)
    {
        if (i < 10) i++;
        else return 0; // Ending program before control reaches the end

        std::cout << "Hello World!\n";
    }
    // But at the end of control, return 0 is not necessary
}
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18