Beginner 8 min

Hello World & Your First Program

Write your first C++ program and understand how compilation works

Learn how to write your first C++ program and understand how it works from the ground up.

A Simple Example

#include <iostream>

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

Breaking It Down

#include <iostream>

  • What it does: Imports input/output capabilities
  • Think of it as: Adding a tool to your toolbox - without it, you can't print to screen
  • Remember: The angle brackets < > mean it's a system library

int main()

  • What it does: Defines the entry point of EVERY C++ program
  • The int part: Promises to return an integer when done
  • Remember: Every C++ program needs exactly one main() function

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

  • std::cout: The output stream (your program's voice)
  • <<: The "insertion operator" - sends data to output
  • std::endl: Adds a newline and flushes the buffer
  • Remember: You can chain << operators together

return 0;

  • What it does: Tells the operating system "program succeeded"
  • 0 = success, any other number = error code
  • Remember: This is how programs communicate success/failure

Why This Matters

  • Every C++ programmer starts here. Understanding the basic structure of a C++ program and the compilation process is fundamental to everything you'll build.

Critical Insight

The main() function is like the front door of your program. No matter how complex your code becomes with thousands of lines across hundreds of files, execution always starts at main() and returns there when done. Think of it like the "Start" button in a video game - everything begins here.

Best Practices

Always use std:: prefix: Write std::cout instead of importing the entire namespace. This prevents naming conflicts in larger projects.

Include proper headers: Only include what you need. #include <iostream> for input/output operations.

Use meaningful indentation: Indent code inside functions consistently (4 spaces or 1 tab) for better readability.

Add return 0 explicitly: Even though modern compilers add it automatically, explicitly returning 0 makes your intent clear.

Common Mistakes

Forgetting semicolons: Every statement needs one ;. Missing a semicolon gives cryptic error messages.

Case sensitivity: Main() is not the same as main(). C++ is case-sensitive everywhere.

Missing return statement: While some compilers allow it, always include return 0; at the end of main().

Debug Challenge

This Hello World program has a bug. Click the highlighted line to see options and fix it:

1 #include <iostream>
2
3 main() {
4 std::cout << "Hello, World!" << std::endl;
5 return 0;
6 }

Quick Quiz

1. What does the main() function return?

An integer (0 for success)
Nothing
A string

2. What does #include <iostream> do?

Prints text to screen
Defines the main function
Includes input/output functionality

3. Which is the correct way to write the main function?

void Main()
int main()
main()

Practice Playground

Try out what you just learned! Write some C++ code and run it to see the results.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once