Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Hello World in C++

Write your first C++ program and see why each of its four lines matters: #include, using namespace std, main, cout, and return 0. Made for day-one students.

BeginnerBasic ProgramsExample 1 of 14
hello-world.cpp
1// This line allows us to use input and output (like printing on the screen)
2#include <iostream>
3using namespace std;
4
5// main() function: this is where every C++ program starts running
6int main() {
7 // This line prints "Hello World" on the screen
8 cout << "Hello World";
9
10 // This line tells the computer that the program ended successfully
11 return 0;
12}

Sample Output

Hello World

The exact text inside the quotes. No newline follows, because the code never prints endl or \n.

In this one

  • What #include <iostream> gives you and why cout fails without it
  • Why every C++ program starts running at main()
  • How cout and the << operator send text to the screen
  • What return 0 tells the operating system

How it works

  1. 1

    The preprocessor pastes in <iostream>, so the names cout and cin become known.

  2. 2

    using namespace std; lets you write cout instead of std::cout.

  3. 3

    The operating system calls main(), the only place execution can start.

  4. 4

    cout << "Hello World"; pushes the characters to standard output.

  5. 5

    return 0; ends main and reports success.

Edge cases

Missing semicolon

Delete the ; after the cout line and build again.

The compiler reports the error on the next line (return 0;), because it only notices the statement never ended when it reaches the next token. Look one line up from the reported line.

No newline at the end

The program prints Hello World with no endl or \n.

Your shell prompt may appear glued to the text, like Hello World$. Add << endl or "\n" if you want the cursor on a fresh line.

What's going on

A C++ Hello World program prints text with cout. Four statements. That's the whole file.

#include <iostream>

Skip this and cout is an unknown name. The compiler dies. iostream is the header that knows how to print (and later, how to read with cin).

using namespace std;

cout lives in std. This line lets you write cout instead of std::cout. Fine in a short file. Bigger projects often skip it and type std:: so names don't collide.

int main() {

Execution starts here. No main, no program. int means it returns a number to the OS. The { } wrap the body.

cout << "Hello World";

The only line that does work. << sends the string to the screen. Drop the quotes and C++ looks for a variable called Hello. Drop the semicolon and the error points at the *next* line, which is annoying.

return 0;

Zero means the process succeeded. You can omit this in modern C++; main still returns 0. Writing it keeps the success code obvious.

Change the string, rebuild, and the output should change. If it does, the toolchain works. Next is a string variable — Display Your Name — then cin for real input.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What happens if you remove #include <iostream>?

Pick an answer to check it.

Question 2

Which function does every C++ program start in?

Pick an answer to check it.

Questions people ask

Do I have to write using namespace std?

No. You can write std::cout every time. Short teaching files use the using line to save typing; larger projects often avoid it so names from different libraries do not clash.

Is return 0 required in main?

In standard C++, reaching the end of main without a return acts like return 0. Writing it is still common because it makes the success code visible.

Why does my program close instantly on Windows?

The console window closes when main ends. Run it from a terminal, or use your IDE run button that keeps the window open.