← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 5 of 15

C++ Syntax & Structure

C++ has strict spelling and punctuation rules, like a language with careful grammar. Statements end with semicolons, curly braces group steps, and capital letters matter.
Syntax
cpp
#include <header>

return_type function_name() {
  statement;
  statement;
}

Semicolons

Every complete statement in C++ must end with a semicolon, which tells the compiler exactly where one instruction stops and the next begins.

Forgetting one is one of the most common beginner errors, and it usually causes the compiler to report an error on the following line instead of the one you actually missed.

Example: Semicolons

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Curly Braces

Curly braces { } group multiple statements into a single block, marking where a function body, loop, or if-statement begins and ends.

Without braces, some constructs like if only apply to the single statement immediately following them, which is a frequent source of subtle bugs when a second line is added later.

Example: Curly Braces

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Check whether true
	if (true) {
		// Print to the console with cout
		std::cout << "Inside the block" << std::endl;
		// Print to the console with cout
		std::cout << "Both lines run" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Tokens

A token is the smallest meaningful piece the compiler recognizes when it reads your source code — keywords like int, identifiers like variable names, literal values, and operators like + are all tokens.

Understanding tokens helps explain compiler error messages, which often point at the specific token where parsing failed.

Example: Tokens

cpp
#include <iostream>

int main() {
	int total = 5 + 3; // int, total, =, 5, +, 3 are all tokens
	std::cout << total << std::endl;
	return 0;
}

Case Sensitivity

C++ treats uppercase and lowercase letters as completely different characters, so main and Main are not interchangeable — only lowercase main is recognized as your program's entry point.

This same rule means total and Total can coexist as two entirely separate variables, which can cause confusing bugs if used carelessly.

Example: Case Sensitivity

cpp
#include <iostream>

int main() {
	int total = 1;
	int Total = 2; // a completely different variable from 'total'
	std::cout << total << " " << Total << std::endl;
	return 0;
}

Indentation

Indentation has no effect on how the compiler parses your code — braces alone define the structure — but consistent indentation is what makes nested loops and conditionals readable to a human.

Most teams enforce a consistent indentation style precisely because the compiler's leniency here makes it easy for code to become unreadable without one.

Example: Indentation

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 2; i++) {
		// Check whether i == 0
		if (i == 0) {
			// Print to the console with cout
			std::cout << "Consistent indentation aids readability" << std::endl;
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon after a statement, so the compiler reports an error on the following line.
  2. Writing Cout or INT instead of cout and int, since C++ is case-sensitive and those names do not exist.
  3. Leaving a { without a matching }, causing errors such as 'expected }' at the end of the file.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.