The First C++ Program: Hello, World!

It is a classic introductory example for learning any programming language, with a simple function: outputting a line of text to the screen:

Hello, World!

βœ… 1. C++ Hello World Program Code

Below is a complete C++ first program that you can run on your computer:

#include <iostream> // Include input-output stream library

int main() {
    std::cout << "Hello, World!" << std::endl;  // Output Hello, World!
    return 0;  // Indicates normal program termination
}

🧠 2. Code Analysis

Let’s understand the meaning of this code line by line:

1. <span>#include <iostream></span>

  • This is a preprocessor directive, not a C++ statement.

  • It tells the compiler: please include the standard input-output stream library (<span>iostream</span>), so that you can use <span>std::cout</span> and <span>std::endl</span>.

  • <span>iostream</span> is part of the C++ standard library, used for handling input (like keyboard) and output (like screen).

2. <span>int main() { ... }</span>

  • <span>main</span> is the entry point function of a C++ program, meaning: every C++ program starts execution from the <span>main</span> function.

  • <span>int</span> indicates that this function returns an integer value, typically used to represent the program’s exit status.

  • <span>{ ... }</span> contains the function body, which is the code that the program will execute.

3. <span>std::cout << "Hello, World!" << std::endl;</span>

  • <span>std::cout</span> is the standard output stream (usually the screen), used to print information to the console.

  • <span><<</span> is the stream insertion operator, used to send content to the output stream.

  • <span>"Hello, World!"</span> is a string constant, which is the text you want to display.

  • <span>std::endl</span> indicates to output a newline and flush the output buffer (equivalent to `

    ` + flush).

πŸ” You can also write it as:

std::cout << "Hello, World!" << '\n';  // Output text + newline, but does not force flush

4. <span>return 0;</span>

  • Indicates that the program has successfully terminated, with the return value <span>0</span> typically representing “no error”.

  • In many operating systems, the return value of the <span>main</span> function can be used to indicate the program’s running status (for example, to the operating system or caller).

In C++, if the <span>main</span> function does not write a <span>return</span> statement, it will default to returning <span>0</span> (C++98 and later standards), but for code clarity, it is recommended to always include <span>return 0;</span>.

▢️ 3. How to Run This Program?

To run this program, you need to follow these steps:

1. βœ… Write Code Using a Code Editor

You can use any of the following tools:

  • Visual Studio Code (with C++ plugins and compiler)

  • Dev-C++ (Windows, suitable for beginners)

  • Code::Blocks

  • CLion (by JetBrains, professional C++ IDE)

  • Visual Studio (Windows, powerful)

  • g++ / clang++ command line tools (Linux / Mac / WSL)

2. πŸ“ Save the Code as a <span>.cpp</span> File

For example, save it as:

hello.cpp

The content is the code above.

3. πŸ”§ Compile the Code

Use a C++ compiler (like g++, clang++, MSVC) to compile the code into an executable program.

β–Ά Compile using g++ on Windows / Linux / Mac (terminal or command line):

Open the terminal (or command prompt / PowerShell / terminal), then type:

g++ hello.cpp -o hello

Explanation:

  • <span>g++</span>: GNU C++ compiler

  • <span>hello.cpp</span>: your source code file

  • <span>-o hello</span>: specify the output executable file name as <span>hello</span> (it may be <span>hello.exe</span> on Windows)

4. β–Ά Run the Program

  • Windows (CMD/PowerShell):

    hello.exe
    

  • Linux / Mac / WSL:

    ./hello
    

You will see the output on the screen:

Hello, World!

πŸŽ‰ Congratulations! You have successfully run your first C++ program!

πŸ†š Other Variations (Simplified Version using namespace std)

To simplify the code, many tutorials will use <span>using namespace std;</span>, so you don’t have to write the <span>std::</span> prefix every time.

The simplified code is as follows:

#include <iostream>
using namespace std;  // Include the standard namespace, so you can use cout directly without std::cout

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

πŸ”’ Note: For beginners, using <span>using namespace std;</span> is acceptable, but in large projects or team collaborations, to avoid naming conflicts, it is recommended to explicitly use the <span>std::</span> prefix (like <span>std::cout</span>).

βœ… Summary: C++ First Program – Hello World

Part

Description

Purpose

Learn the basic structure of a C++ program, output a line of text to the screen

Code

See above

Core Knowledge Points

<span>#include</span>, <span>int main()</span>, <span>std::cout</span>, <span>return 0</span>

How to Run

Compile and run with g++ / clang++ / Visual Studio, etc.

Output

<span>Hello, World!</span>

🧩 Want to Go Further?

After learning Hello World, you can try:

  1. Output your own name:

    std::cout << "Hello, I am Xiao Ming!" << std::endl;
    

  2. Output multiple contents:

    std::cout << "Hello, " << "World!" << std::endl;
    

  3. Receive user input (next learning step):

    Use <span>std::cin</span> to read input from the keyboard, such as prompting the user to enter their name and then printing it out.

Leave a Comment