Every program you submit this year will start with the same handful of lines. This lesson takes them apart one at a time, so that from here on they are something you understand rather than something you copy.
From text to a running program
A C++ program starts life as plain text in a file — main.cpp, say. The
computer can't run text, so a program called a compiler translates it first:
Step 2 is the one that surprises people: a single mistake anywhere in the file means nothing runs, not even the lines above it.
The first program
Six lines, and every one of them has a job:
| Line | What it does |
|---|---|
#include <bits/stdc++.h> | Brings in the whole standard library — input, output, text, maths — in one line. |
using namespace std; | Lets you write cout instead of std::cout. |
int main() { | Where the program starts. Everything between { and } runs from top to bottom. |
cout << "Hello, World!" | Sends the text in quotes to the screen. cout is console output. |
<< "\n" | Ends the line, so whatever comes next starts on a new one. |
return 0; |