The famous C plus plus Hello World Program Explained in Excruciating Detail

The famous C plus plus Hello World Program Explained in Excruciating Detail

By state law, every programmer learning a new computer language is obligated to learn the Hello World program. We present the C++ (C Plus Plus) version of The Famous Hello World Program. In excruciating detail, we analyze each line of the program and systematically remove all the fun from computer programming.

  * The Hello World Program                      main.cpp *
  * Name: nicomp                                          *
  * Abstract: My first C++ program.                       *
  * It displays Hello World on the screen                 *
  * Target Compiler: Microsoft C++ / Visual Studio        *
  * Revision History:                                     *
// Drag in a header. 
// This header is provided by Microsoft as part of Visual Studio
#include <iostream>

//Specify the default namespace. Most stuff in iostream is in the std namespace
using namespace std;

// Entry point. There has to be a main() function.
void main()
//	Write to the standard output device
	cout << "\n Hello World \n\n";

Lines 1 through 8

Lines 1 through 8 of our programming masterpiece consist of comments. This particular program doesn't cry out for copious comments, however we emphasize documentation at every level of programming.

At a minimum, we prefer enough commenting to make the program self-identifying. As a rule of thumb; name, description and revision history should be included.

Visual Studio Screen Captures

Code in Visual Studio. The project builds with no errors or warnings.
Code in Visual Studio. The project builds with no errors or warnings.
Warning Level Set to maximum.
Warning Level Set to maximum.
The output generated by the program.
The output generated by the program.

Lines 9 through 11

Lines 9 through 11 describe and implement the preprocessor directive "include". An include statement is processed before the code is digested by the C++ compiler. The contents of the file called "iostream" are copied into the program before compilation.

The iostream is provided by the compiler vendor.

Line 12

Line 12 is blank. C++ ignores blank lines during compilation. Programmers may use blank lines to make a program more readable.

We warned you that this tutorial provides excruciating detail.

Lines 13 and 14

Lines 13 and 14 describe and specify the default namespace that should be assumed during the compilation process. A namespace is a conceptual mechanism that guides the compiler. Symbolic references identified by the compiler during the compilation process will be resolved by searching through the default namespace. In this particular example, the cout symbol is found in the default namespace.

Line 15

Refer to Line 12.

Lines 16 and 17

Lines 16 and 17 describe and identify the entry point for the program. In other words, the execution of the program starts here (line 17).

Contrary to popular opinion. line 17 is perfectly valid and will not generate a compiler warning or compiler error. Another version of line 17 might be:

void main()

Other incarnations also exist.

Editor's note: this is not a religious issue. Don't beat yourself up if your entry point doesn't look exactly like mine.

Lines 18 and 21

Lines 18 and 21 delimit the code in the main() function. C++ uses curly braces.

Lines 19 and 20

Lines 19 and 20 define and implement the only executable code in the entire program. Using the cout object, we write a message to the device commonly referred to as stdout. Also called Standard Out, this device is by default connected to console window. Most operating systems permit stdout to be redirected to any other output device such as a disk file or a printer.

More by this Author

Comments 8 comments

nicomp profile image

nicomp 7 years ago from Ohio, USA Author

@Adam: That's a great resource. Thanks! I am adding it to my syllabus now.

A Texan 7 years ago

Uhhhhhhhhhhhhhhhh, what? I got lost a C++!

nicomp profile image

nicomp 7 years ago from Ohio, USA Author

@A Texan: It's all good. Thanks for stopping by!

ManinaBlueSuit profile image

ManinaBlueSuit 5 years ago from Colorado

I have tried programs like this before, but I need to add


return 0;

to the end of mine or it won't work. Did you accidently leave it out or does your program somehow work without that?

nicomp profile image

nicomp 5 years ago from Ohio, USA Author

@ManinaBlueSuit : My program is complete, but the window in which your output appears may be closing so fast that you don't see the output. The code you added would cause the program to block on stdin until a key is pressed, which keeps the window open. This is a function of the dev environment you are using, not the program itself.

ManinaBlueSuit profile image

ManinaBlueSuit 5 years ago from Colorado

@nicomp: Ah, so if used something other than Dev C++, I would not need to use system("pause"). That is good to know. This makes me wonder if there is anything else special I must do with Dev to make it work properly.

nicomp profile image

nicomp 5 years ago from Ohio, USA Author

@ManinaBlueSuit : Your program works properly either way, with or without the pause statement. There's nothing wrong with Dev C++. The original program, as written, writes to stdout and does not deal with windows: your IDE launches the program in a window and when the program exits, the window closes.

Putting in the pause statement causes the window to remain open because the program is blocked on stdin. When the program unblocks (when you press a key) the program ends and the window closes.

Bill 5 years ago

@ManinaBlueSuit there are other methods instead of using

system("pause") there is even a method that can be used with iostream ;) system("pause") is actually bad from my understanding the way its called from os and its also os specific

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

    Click to Rate This Article