C++ Syntax & Structure

Scope: This lesson dissects the building blocks of every C++ file: tokens, statements, blocks, comments, identifiers, keywords, and the main function. By the end you will read any beginner C++ program with confidence.

Tokens

A C++ source file is a long sequence of characters, but the compiler does not read it letter by letter. It first groups characters into tokens — the smallest meaningful units. There are five kinds of tokens:

  • Keywords — reserved words with fixed meaning: int, return, if, class.
  • Identifiers — names you invent: main, count, temperature.
  • Literals — constant values written directly: 42, 3.14, "hello", true.
  • Operators — symbols that perform actions: +, -, <<, ==.
  • Punctuation — structural symbols: ;, {, }, (, ), ,.

Whitespace and newlines separate tokens but are otherwise ignored. That is why indentation is optional in C++ — you indent to help humans, not the compiler.

Statements & Blocks

A statement is one complete instruction, and it ends with a semicolon ;. A program is a sequence of statements executed from top to bottom.

int age = 25;                  // declaration statement
age = age + 1;                 // assignment statement
std::cout << age;              // expression statement (action)

Blocks group statements

Curly braces { } group several statements into one block. Blocks appear everywhere in C++: after if, inside loops, and as the body of functions. A block behaves as a single unit — either all of it runs, or none of it.

{
    int x = 10;              // x exists only inside this block
    int y = x * 2;
    std::cout << y;
}                            // x and y are gone after this line

Variables declared inside a block are only visible inside it. This is called scope and it will matter more when you write functions.

Comments

Comments are text the compiler ignores — they exist for human readers. Good comments explain why a line exists, not what the code obviously does. C++ has two styles:

// single-line comment: everything after // to the end of the line

/* multi-line comment: everything
   between the opening and closing stars */
  • Use // for short explanations next to code.
  • Use /* */ for longer notes or temporarily disabling code.
  • Never state the obvious — int x = 5; // set x to 5 adds no information.

Identifiers & Keywords

An identifier names a variable, function, or type. The rules are simple:

  • Letters, digits and underscores are allowed; the first character must be a letter or underscore.
  • Identifiers are case-sensitive: Score and score are different names.
  • Keywords are reserved — you cannot name a variable int or return.

Common keywords you will meet soon: int, double, bool, char, void, if, else, while, for, class, return, const, namespace, using. The full list is fixed by the standard.

Includes & Main

Two elements appear in nearly every C++ program: an #include directive that loads a library, and a main function that starts execution.

#include <iostream>   // load the standard input/output library

int main() {          // program entry point
    return 0;         // report success to the operating system
}

The #include line is processed before compilation begins (you saw this in the toolchain lesson). It copies the declarations of the iostream library into your file, which makes std::cout available.

The main function

main is special: the operating system calls it when your program starts, and execution ends when it finishes. Every C++ program has exactly one main. The int before it declares that the function delivers an integer result.

Return values

return 0; sends the value 0 back to the operating system. By convention, zero means success; any other value signals an error. You can also write return EXIT_SUCCESS; for the same result.

Console Output

std::cout is the standard output stream — it sends text to the terminal. The operator << pushes a value into the stream. You can chain several values in one statement:

std::cout << "The answer is " << 42 << ".\n";
// prints: The answer is 42.

The \n inside the string is an escape sequence meaning "new line". Without it, everything would print on one long line. Two more useful escapes: \t for a tab and \" to print a double quote inside a string.

The std:: prefix tells the compiler that cout lives in the std namespace — the standard library's container for names.

A Complete Program

Combining everything from this lesson, here is a program that uses variables, comments, output, and the main function together.

// student.cpp — introduces a student.
#include <iostream>

int main() {
    std::string name = "Ada";   // a text variable (needs , see below)
    int grade = 95;             // a whole number variable

    std::cout << "Student: " << name << "\n";
    std::cout << "Grade:   " << grade << "\n";
    return 0;
}

One detail: std::string is declared in the <string> header, so a strict build needs #include <string> too. The next lesson on data types explains variables in depth.

Practice

Train your eye on structure. Type each exercise, compile with g++ -std=c++17 -Wall -Wextra, and read every message.

  1. Write a program that prints your name and age using two std::cout statements.
  2. Add a comment above each statement explaining its purpose.
  3. Remove every semicolon from one statement, compile, and find the reported line.
  4. Rename main to Main, compile, and read the linker error about an undefined main.