- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
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
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.