1.10.1. Hello, World!

Time: 00:03:22 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

Brian Kernighan and Dennis Ritchie introduced C to the wider programming world with their book The C Programming Language. Their first program in the book simply printed Hello, World! on the computer screen. Following what has become a longstanding tradition, we will also begin by writing the quintessential hello-world program but in C++.

Functions

Every C++ program contains exactly one function named main. Ignoring some minor details, for now, main is where every C++ program begins execution. (The name main is not a keyword, but we typically treat it like it is - we don't use it for anything else in the program.) All functions, even small, simple ones like main, can be broken down into a few parts, and you should start learning the part names now.

A picture showing the parts of a function as described in the caption.
The components of a C++ function. All C++ functions follow the same basic pattern: Exactly one function in each program is named "main." Programmers are free to choose any appropriate name for the other functions. Programmers write the function's parameter list and body appropriately to solve a given problem.

Hello, World!

Programmers can write most programs in many ways, and this is true even of a program as small and as simple as Hello, World!. The following versions of the Hello World program demonstrate three commonplace and accepted programming styles. The examples illustrate where programmers place the opening and closing braces and how the code is indented.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!" << endl;
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
 
(a)(b)
#include <iostream>
using namespace std;

int main()
    {
    cout << "Hello, World!" << endl;
    return 0;
    }
(c)
Commonplace programming styles. The C++ compiler does not use indentation to check syntax or generate machine code - indentation conveys no meaning to the C++ compiler. While each preprocessor directive must be on a line by itself, line breaks are insignificant within the C++ statements. You could write the entire program, except the preprocessor directives, on a single line or break each line anywhere that whitespace is legal.
  1. Braces atop each other and aligned with the outer structure
  2. Opening brace at the end of the outer-structure line; closing brace below and aligned with the outer structure
  3. Braces atop each other and aligned with the inner structure

These are just a few of the many variations programmers have advocated, debated, and accepted. See Indentation style for more examples of indentation and brace placement. Regardless of how you indent or where you place the braces, the most important thing to remember is that

Important:

The indentation helps people read the code. The idea is for the physical layout to reflect the logical organization of the program. Studio will automatically take care of most of the indentation for you, but there is one practice that you can adopt that will help improve your code formats: always use the same key or keys to indent. Different editors indent different amounts when displaying a TAB character (i.e., they set different tab stops). Suppose your editor sets tab stops every four characters, and suppose you indent some lines with a tab and some with four spaces. Your code will look "ragged" when viewed with an editor that sets eight-character tabs.

 

Python Programmers

C++ uses indentation to make the code easier for humans to read. Programmers add indentation so that the code's physical layout reflects its logical structure. You must use braces to create the same structures you would create with indentation in a Python program.

What Does It All Mean?

Each line of the program has a specific purpose and contributes in some way to the overall problem solution. Understanding what each line does will help you extend this simple program to more realistic and complex programs.

#include <iostream>
Copies the declarations for the input and output operations into the current program file. Note that this line does not end with a semicolon. Please see the previous discussion of the preprocessor for more details about how this directive works.
using namespace std;
This statement is similar to the "import" statement used in Java, which organizes its library or API components into packages. Without the "import" statement, every use of a library component in a Java program would require a much longer reference to access the package. The "import" statement allows Java programs to access the library components with a more compact shorthand notation. Similarly, C++ organizes its library components into namespaces. Without the "using" statement, access to a C++ library component would require a longer reference. But the "using" statement allows a more compact, simplified shorthand notation.
int main()
This statement defines a function named main, that has a return type of int and an empty argument list. Everything contained between the opening { and the closing } constitute the body of function main.
cout << "Hello, World!" << endl;
This statement writes the string "Hello, World!" to the console screen. cout is an output stream object (data is treated as a simple stream or sequence of bytes as it is written out of or read into a program) that is attached to the console (the name is a shortened form of console output). << (the inserter operator) collects the data on its right and sends or inserts it into the stream object on its left.
return 0;
The return statement terminates the program (i.e., it causes the program to stop running). The value following the return, "0" in this case, is called the exit status. A zero exit status indicates that the program is terminating or exiting without error; a non-zero exit status indicates that the program is terminating with an error. The program returns the exit status to the operating system where shell scripts or batch files may use it. We'll revisit the return operator in Chapter 2.