Lesson 2 of 50 beginner

Setting Up Your Coding Playground

Get your tools ready — let us write and run your very first C++ program!

Open interactive version (quiz + challenge)

Real-world analogy

A compiler is like Google Translate, but for computers. You write code in C++ (a language humans can read), and the compiler translates it into machine language (a language the computer understands). Without a compiler, talking to your computer would be like trying to order food in a country where nobody speaks your language!

What is it?

Setting up your coding environment means getting the tools you need to write, compile, and run C++ programs. You can use free online editors (like ideone.com or onlinegdb.com) that work right in your browser, or install a C++ compiler (g++) on your own computer. Either way, within 5 minutes you will be ready to write and run code!

Real-world relevance

Every app on your phone, every website you visit, and every video game you play was written as code and then compiled (translated) into a program. The compiler is one of the most important inventions in computer science. Without compilers, programmers would have to write in binary (just 1s and 0s) — which would be like writing an essay using only dots and dashes!

Key points

Code example

#include <bits/stdc++.h>  // Include all standard libraries
using namespace std;       // Lets us use cout and endl directly

int main() {               // Every C++ program starts here
    cout << "Hello, World!" << endl;          // Print text to the screen
    cout << "My name is a CP beginner!" << endl;  // Another line of output
    cout << "I am learning C++ for competitive programming." << endl;
    cout << "1 + 1 = " << 1 + 1 << endl;     // C++ can do math and print the result!
    cout << "Let us goooo!" << endl;          // endl moves to the next line
    return 0;              // Tells the computer the program finished successfully
}

Line-by-line walkthrough

  1. 1. #include — this line says 'give me access to ALL the tools in C++.' It is like walking into a hardware store and saying 'I will take one of everything!'
  2. 2. using namespace std; — this is a shortcut that lets us write 'cout' instead of 'std::cout'. It saves us from typing 'std::' in front of everything. Less typing = more time for solving problems!
  3. 3. int main() { — every C++ program starts here. The computer looks for main() and begins running your code from this point. Think of it as the front door of your program.
  4. 4. cout << "Hello, World!" << endl; — cout (pronounced see-out) sends text to the screen. The << means 'send this.' endl means 'go to the next line.' The semicolon at the end is like a period at the end of a sentence — every statement in C++ needs one!
  5. 5. cout << "My name is a CP beginner!" << endl; — another line of output. You can have as many cout lines as you want. Each one prints something to the screen.
  6. 6. cout << "1 + 1 = " << 1 + 1 << endl; — here is something cool: you can mix text and math! The text part "1 + 1 = " is printed as-is, and then 1 + 1 is calculated by the computer and printed as 2. So the output is '1 + 1 = 2'.
  7. 7. return 0; — this tells the computer 'the program is done, and everything went fine.' The 0 means 'no errors happened.' It is like saying 'The End' at the end of a movie.
  8. 8. The closing } — this curly brace matches the opening { after int main(). Everything between these two braces is your program. Forgetting a closing brace is a very common error!

Spot the bug

#include <bits/stdc++.h>
using namespace std;

int main() {
    cout << "Hello World!" << endl
    cout << "I love CP!" << endl;
    return 0;
}
Need a hint?
Every statement in C++ must end with something important. Look at the end of the first cout line very carefully.
Show answer
The first cout line is missing a semicolon at the end! It should be: cout << "Hello World!" << endl; (with a semicolon after endl). In C++, every statement MUST end with a semicolon. It is the most common beginner mistake — even experienced programmers forget semicolons sometimes!

Explain like I'm 5

Imagine you only speak English, and your computer only speaks Robot Language. You need a translator to help you talk to each other. The compiler is that translator! You write your instructions in C++ (which is close to English), the compiler translates it to Robot Language, and then the computer can follow your instructions. Cool, right?

Fun fact

The very first 'Hello World' program was written in 1972 by Brian Kernighan while he was writing documentation for the B programming language (the ancestor of C). Since then, literally billions of programmers have started their journey by writing Hello World. You are now part of that incredible tradition!

Hands-on challenge

Open ideone.com (or onlinegdb.com) in your browser. Select C++ as the language. Type the Hello World program from this lesson and click Run. Then modify it to also print your name and your favorite food. For example: 'My name is Alex and I love pizza!' Take a screenshot of your output — you just ran your first program!

More resources

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero