Understanding First C++ Program

Last Updated : 7 Aug, 2026

The Hello World program is the first program most programmers write while learning C++. It introduces the basic structure of a C++ program and demonstrates how to display output on the console.

  • Demonstrates the basic syntax of a C++ program.
  • Introduces essential program components such as main(), #include, and cout.
  • Helps beginners understand the execution flow of a C++ program.
C++
#include <iostream>
using namespace std;

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

Output
Hello World

Note: You can run this program either by setting up a local C++ development environment or by using an online C++ compiler.

Components of the First C++ Program

Below is the explanation of every line and the components of the above program.

prim_s_algorithm_14

Preprocessor Directives

"#include" is a preprocessor directive that tells the compiler to include the content of a file in the source code. For example, #include<iostream> tells the compiler to include the input-output library which contains all C++'s input/output library functions.

#include <iostream>

Header Files

A header file contains functions, global variables and macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor. Header files generally ends with the .h extension although in modern C++ many header files do not have the .h extension

#include <iostream> // Modern C++ header

#include <cstring> // C++ version of string.h

Namespace

Namespaces are used to group logically related functions, variables and other entities to avoid name conflicts. The line "using namespace std" is used to import the entity of the std namespace into the current namespace of the program. It is basically the space where all the inbuilt features of C++ are declared.

For example: std::cout is an object of the output stream (ostream) used to display output on the console.

using namespace std;

Main Function

The main() function is the entry point of every C++ program when the program is executed the code written in the main functions is executed first. The opening braces '{' indicates the beginning of the main function and the closing braces '}' indicates the ending of the main function.

int main()

{

// Program statements

}

Comments

The comment are used to display additional information about the programthat do not contain any programming logic and are not the part of executable program and are purely used for documentation and provide explanation or notes about the code. When a comment is encountered by a compiler, the compiler simply skips that line of code.

// C++ program to print "Hello World"

Output Statement

The cout is a output statement in C++ that is used to display output on the console screen. Everything followed by the character << in double quotes " " is displayed on the output screen. The semi-colon character at the end of the statement is used to indicate that the statement is ending there.

cout << "Hello World";

Return Statement

The return statement is used to return a value from a function and indicates the finishing of a function. Here, it is used to sent the signal of successful execution of the main function.

return 0;

Code Indentation

Proper indentation makes programs easier to read and maintain.

int main() {

cout << "Hello World";

return 0;

}

Following consistent indentation and using meaningful comments are considered good programming practices and help improve code readability and maintainability.

Comment