This Program Has Been Tested: It Works Perfectly!

... or at least that's what I was told when I found this implementation of Hello World in C++:

#include <iostream.h>

main()
{
    cout << "Hello World!";
    return 0;
}

Stashed away at the bottom of the page in the section "Program Notes" is this helpful bit of information:

This program has been tested, it works perfectly without any errors or warnings.

... and Yet, GCC Complains

You may be wondering to yourself "What on Earth prompted them to include that little tidbit of information?". Even in C++ Hello world should not be too hard to get right:

aaron@athena:~/scratch$ g++ hello.cpp
In file included from /usr/include/c++/4.2/backward/iostream.h:31,
                 from hello.cpp:1:
/usr/include/c++/4.2/backward/backward_warning.h:32:2: warning:
    #warning This file includes at least one deprecated or antiquated
    header. Please consider using one of the 32 headers found in section
    17.4.1.2 of the C++ standard. Examples include substituting the <X>
    header for the <X.h> header for C++ includes, or <iostream> instead
    of the deprecated header <iostream.h>.
    To disable this warning use -Wno-deprecated.

... but GCC still complains. The specifics of what it's complaining about are euclidated nicely in this article but gist is that <iostream.h> references Bjarne Stroustrup's original implementation which is only included (if it's included) in modern compiler distributions for compatibility reasons whereas <iostream> is the standardized version which is better, faster, cheaper, more portable and should be used in preference to <iostream.h> whenever possible.

After switching to <iostream> and informing the compiler that we're using namespace std GCC will happily compile and run our simple program:

aaron@athena:~/scratch$ g++ hello.cpp
aaron@athena:~/scratch$ ./a.out
Hello World!aaron@athena:~/scratch$

For stylistic reasons, I'll add an endl at the end of the message, but that's not fatal. Unfortunately, I'm not quite done. As someone who thinks that my compiler might know a thing or two about code, I like to turn on all of the warnings that my compiler is able to give me. Anything that the compiler is worried enough to raise a warning over is probably worth looking into. Compiling with -Wall -pedantic shows us that GCC is still not happy:

aaron@athena:~/scratch$ g++ -Wall -pedantic hello.cpp
hello.cpp:5: error: ISO C++ forbids declaration of ‘main’ with no type

... easily enough fixed, just add an int return type to the main function.

All Fixed Up

After changing the iostream header, adding a namespace declaration, adding a return type and a newline (which is a surprising number of things for "Hello World!" when you think about it) we can now compile with -Wall and -pedantic and GCC won't emit so much as a peep:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
aaron@athena:~/scratch$ g++ -Wall -pedantic hello.cpp
aaron@athena:~/scratch$ ./a.out
Hello World!

The Moral of the Story

When your students send you email complaining about your implementation of "Hello World" emitting a slew of obscure errors when compiled, posting a note indicating that your program has been compiled and tested and is bug free is probably not the most productive thing you could do.