Search⌘ K

Anatomy of a "Hello World!" Program

Explore the fundamental structure of a C++ Hello World program to understand how each part works. This lesson covers preprocessor directives, namespaces, the main function, and how to display output on the console, preparing you to write and run your own basic C++ code.

“Hello World!” program

Here is the source code for a program that prints “Hello World!” on the screen. Look at the program, and then we will discuss every component of the program in detail.

Run the code below and see the output!

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

Explanation

When we run the code above, it prints “Hello World!” on the screen. This means that we can modify the code given above to print anything on the screen. Sounds interesting!

But first, let’s understand the meaning of each line in a “Hello World!” program.

Preprocessor directives

Line No. 1: Lines that start with # are known as preprocessor directives. Preprocessor directives tell the compiler to preprocess some information before starting the compilation. The Header file contains the declarations of predefined functions in C++.

#include is a preprocessor directive. It tells the preprocessor to add the content of the iostream header file ...