Menu

C++ Syntax: Statements, Semicolons, Braces, and main()

The core syntax rules of a C++ program - statements and semicolons, curly-brace blocks, the main function, and how output works - explained from a first program.

This page includes runnable editors - edit, run, and see output instantly.

From "It Compiles" to "I Can Read It"

In the previous page you took a source file and turned it into a running program with a compiler. Now we'll slow down and look at what was actually in that file. Every C++ program is built from the same handful of structural pieces, and once you can name them, an unfamiliar .cpp file stops looking like noise.

Here is a complete, runnable program. Read it once, then we'll take it apart line by line.

Five meaningful lines, and every one of them is doing a specific job. Let's go through them.

The #include Line

#include <iostream>

This line pulls in the <iostream> header so you can use input/output features like cout. The #include is a preprocessor directive - it runs before the real compilation and essentially pastes the contents of that header into your file.

Two things make #include lines special:

  • They start with #, which marks them as preprocessor directives.
  • They do not end with a semicolon. This is the one common exception to the "every line ends in ;" habit you're about to build.

If you use cout but forget #include <iostream>, the compiler won't know what cout is and you'll get an error like 'cout' was not declared in this scope. The fix is almost always a missing include.

The main Function

int main() {
    // ...
    return 0;
}

main is where your program begins. When you run the compiled program, execution starts at the first line inside main and stops when main returns. Every C++ program has exactly one main - no more, no less.

The pieces:

  • int - main hands an integer back to the operating system. By convention, 0 means "finished successfully."
  • main() - the name, followed by parentheses for its parameters (empty here).
  • { ... } - the curly braces wrap up the body: the statements that run.
  • return 0; - ends main and reports success. If you leave it out, C++ treats main as if it returned 0 for you - but writing it makes your intent obvious.

Statements and the Semicolon

A statement is one complete instruction. In C++, every statement ends with a semicolon:

C++ ignores line breaks and extra spaces entirely. The compiler doesn't care whether you put statements on one line or ten - the ; is what tells it where each statement ends. All three of these are identical to the compiler:

int a = 1; int b = 2;
int a = 1;
    int b = 2;
int
a
= 1;

That freedom comes with the most common beginner trap: a forgotten semicolon. Watch what happens here.

int x = 5    // <-- no semicolon
int y = 10;

You'll get an error like expected ';' before 'int'. Notice the gotcha: the compiler only realizes something is wrong when it reaches the next line, so the reported line number often points one line below the actual mistake. When an error mentions a missing ;, check the line above the one in the message.

Curly Braces Define Blocks

Where some languages use indentation to group code, C++ uses curly braces { }. Everything between a matching pair of braces is one block. Function bodies, loops, and if statements all use them:

The two lines inside the if's braces run only when the condition is true. The last cout is outside the braces, so it always runs.

Because braces - not indentation - define structure, C++ doesn't force you to indent. But you should anyway: indentation is how humans read the structure that the braces define. A common bug is an unmatched brace - every { needs a closing }. If you forget one, the compiler usually reports an error near the end of the file (expected '}' at end of input), far from where you actually slipped. Counting your braces, or letting your editor match them for you, saves a lot of confusion.

Output with cout and <<

You've seen cout a few times now. It's the standard output stream, and you send values to it with the << operator (read it as "put this into the output"):

You can chain << to print several things in a row. endl ends the line (it also flushes the buffer); the escape sequence "\n" is a lighter-weight way to print a newline. For simple programs either is fine.

About that using namespace std; at the top: names from the standard library live in the std namespace, so the full name is std::cout. Writing using namespace std; once lets you drop the std:: prefix and just write cout. It's convenient in short examples, but in larger projects many teams prefer the explicit std::cout to avoid name clashes - so don't be surprised to see it written either way.

You Can Now Read the Skeleton of Any Program

Includes at the top, a main function as the entry point, statements ending in semicolons, braces grouping code into blocks, and cout << for output - that skeleton is in nearly every C++ file you'll ever open. The rest of the language is detail layered on top of these few shapes. When a program looks intimidating, find main first, then read the statements in order; the structure will guide you.

Next: Comments

Right now every line in your programs is code the compiler reads. Next we'll add lines it deliberately ignores - comments - so you can leave notes for yourself and other readers. You'll see the // single-line form and the /* ... */ block form, plus when each one is the right choice.

Frequently Asked Questions

Why does every C++ statement need a semicolon?

C++ ignores line breaks and whitespace, so it needs an explicit marker for where one statement ends and the next begins. That marker is the semicolon ;. A missing semicolon is the single most common beginner compile error - and the reported line number is often the line after the one you forgot it on.

What does int main() do in C++?

main is the function the program starts running from - execution begins at its first line and ends when it returns. Every C++ program needs exactly one main. The int means it hands back an integer status code to the operating system, where 0 conventionally signals success.

Do I have to write std:: before cout?

Either write std::cout (fully qualified) or add using namespace std; once at the top and then just write cout. The qualified form is safer in larger files; using namespace std; is a common shortcut in small examples and tutorials.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED