ProgramGuru

Your First C++ Program

Your First C++ Program

The iostream header declares standard input/output facilities.

The main function starts this example, and std::cout sends text to standard output; the newline ends the displayed line.

Before this lesson, review Set Up a C++ Compiler when needed.

What you’ll learn

  • Use hello world to your First C++ Program.
  • Explain how the complete example produces Hello, World!.
  • Make the practice change and explain why the result changes or stays the same.

Follow a complete C++ example

The main function starts this example, and std::cout sends text to standard output; the newline ends the displayed line.

Read the declarations above main before tracing its statements. Each declared name supplies the value, type or operation used later. The std:: prefix selects a name from the standard library, and cout << sends a value to the output stream.

C++17 · main.cpp · complete program

#include <iostream>

int main()  {
  std::cout << "Hello, World!" << '\n';
  return 0;
}

Expected output

Hello, World!
Run in C++ compiler →

Connect the code to its result

C++17 · source used in this explanation

#include <iostream>

int main()  {
  std::cout << "Hello, World!" << '\n';
  return 0;
}

Expected output

Hello, World!

The iostream header declares standard input/output facilities. The main function starts this example, and std::cout sends text to standard output; the newline ends the displayed line.

Trace the result: Read the complete program below from its starting values to the output operations. Each std::cout statement displays the value supplied at that point; \n begins the next line. The resulting lines, in order, are Hello, World!. In this example, distinguish what the program calculates or changes from what it actually displays.

Important mistakes and boundaries

A semicolon ends a statement; braces delimit a body. Changing source does not change an old executable until it is rebuilt.

Change the school example

C++17 · edit this complete example

#include <iostream>

int main()  {
  std::cout << "Hello, World!" << '\n';
  return 0;
}

Your task: Replace Hello, World! with Welcome to school. Before running your edited program, predict the new output and explain which stored value, condition or object changes. Keep the rest of the example intact.

Use the complete source repeated here; this task does not depend on remembering code from another section.

Worked answer and reasoning

Worked variation: Replace Hello, World! with Welcome to school. Rebuild and the new greeting prints.

The iostream header declares standard input/output facilities. The main function starts this example, and std::cout sends text to standard output; the newline ends the displayed line.

A semicolon ends a statement; braces delimit a body. Changing source does not change an old executable until it is rebuilt.

For a baseline run, save this source as main.cpp and use clang++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o school, or equivalent g++ flags. Recompile after editing so that you run the updated executable.

The source and output below belong to the edited program. Compare them with the original example to see the consequence of this single change.

C++17 · worked variation · complete program

#include <iostream>

int main()  {
  std::cout << "Welcome to school" << '\n';
  return 0;
}

Expected output

Welcome to school
Run in C++ compiler →

Check your understanding

C++17 · original program for this question

#include <iostream>

int main()  {
  std::cout << "Hello, World!" << '\n';
  return 0;
}
Before making the practice edit, what does the original C++17 program print?
Choose one answer. You can try again.

Answer and explanation

Hello, World!

The iostream header declares standard input/output facilities. The main function starts this example, and std::cout sends text to standard output; the newline ends the displayed line. A semicolon ends a statement; braces delimit a body. Changing source does not change an old executable until it is rebuilt.

Writing Your First C++ Program

Create a file named hello.cpp. In C++, the filename can be anything, but it's a good practice to use something meaningful. Here, we use hello.cpp for our Hello World example.

#include <iostream>

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

Compiling the Program

Open a terminal or command prompt where your hello.cpp file is saved and run the following command:

g++ hello.cpp -o hello

What happens during compilation?

The g++ command invokes the GNU Compiler Collection for C++. Here's what it does:

  • Reads your .cpp source file — written in human-readable C++ code.
  • Checks for syntax errors and compiles it into machine code.
  • Generates an executable file named hello.

This executable is now ready to be run on your operating system.

Running the Program

To run the compiled program, use the following command in your terminal:

./hello

What happens during execution?

The system executes the compiled machine code in the hello binary:

  • It loads the program into memory.
  • Starts execution from the main function.
  • Prints Hello, World! to the console.
Hello, World!

You’ve now compiled and executed your first C++ program — a foundational step in learning the C++ programming language.

Common Issues to Watch For

  • Missing semicolons: C++ statements must end with ;.
  • Namespace issues: Use std:: or add using namespace std; for convenience.
  • Missing headers: Functions like std::cout require #include <iostream>.

What to remember

The iostream header declares standard input/output facilities. A semicolon ends a statement; braces delimit a body. Changing source does not change an old executable until it is rebuilt.

Useful connections: Set Up a C++ Compiler, Give a Task a Name.

Language and design guidance: C++ Core Guidelines and C++ working draft. The draft describes an evolving language; these examples deliberately target C++17.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Read Statements and Comments →