Loading lessons...
C++ Syntax
C++ Syntax
Every C++ program shares the same skeleton. Once you know it, you can read almost any beginner program.
The classic full program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
The building blocks
#include <iostream>- a "header" line. It pulls in the library that gives us input and output tools.using namespace std;- names likecoutlive in a group (a namespace) namedstd. This line lets us writecoutinstead ofstd::cout.int main()- the entry point. The computer starts executing here, and this is the function it searches for.{ }- curly braces wrap the code into a block: the lines that run together as the body ofmain.cout << "Hello World!" << endl;- an output statement that prints text to the console.return 0;- an ending signal meaning "all done, successfully".
Two rules to remember
- Statements end with a semicolon
;. Almost every line of C++ does work finishes with one. - Code groups into blocks using
{ }. The braces tell where a list of statements begins and ends.
Why this order?
The program reads top-down: pull in tools, prepare names, open main, run statements, and say goodbye with return 0;. That rhythm is the C++ "syntax skeleton".
TL;DR
- Skeleton: header,
using namespace std;,main(), a block,return 0;. #includeloads tools;mainis where execution starts.{ }groups a block of statements.- Statements finish with a semicolon
;.