C++ Syntax
Learn the rules of writing C++ — statements, blocks, comments, semicolons, and the structure every program follows.
Every C++ program follows this skeleton:
cpp#include <iostream> // 1. Headers using namespace std; // 2. (Optional) namespace alias int main() { // 3. Entry function // 4. Statements go here cout << "Hi"; return 0; // 5. Exit code }
A statement is one instruction. Every C++ statement ends with a semicolon ;.
cppint x = 10; // statement 1 cout << x << endl; // statement 2
Forgetting ; is the #1 beginner error — expected ';' before '}'.
Code surrounded by { } is a block. Functions, loops, ifs, classes — all use blocks.
cppif (x > 0) { cout << "positive"; cout << " number"; }
Two flavours:
cpp// Single-line comment /* Multi-line comment */
Comments are ignored by the compiler. Use them to explain why, not what.
C++ is case-sensitive:
cppint Age = 21; cout << age; // ERROR — 'age' not declared (it's 'Age')
- Start with a letter or underscore:
score,_total,userName. - Can contain letters, digits, underscores:
score1,total_amount. - Cannot start with a digit: ❌
1score. - Cannot be a keyword: ❌
int, ❌return.
int, float, double, char, bool, void, if, else, for, while, do, switch, break, continue, return, class, struct, public, private, protected, new, delete, this, const, static, namespace, using, template, typename.
cppcout << "Hello"; // print text cout << 42; // print number cout << "Sum = " << 10+5; // chain cout << endl; // newline + flush cout << "\n"; // newline (faster)
cppint age; cout << "Enter age: "; cin >> age; // read integer cout << "You are " << age;
cpp#include <iostream> using namespace std; int main() { string name; int age; cout << "Name? "; cin >> name; cout << "Age? "; cin >> age; cout << "Hello " << name << ", you are " << age << " years old." << endl; return 0; }
Output (for input Aarav 21):
Hello Aarav, you are 21 years old.
- Missing semicolon —
cout << "Hi"without;triggers a confusing compiler error on the next line. - Wrong arrow direction —
cin << age;(wrong, should becin >> age). - Reading without
#include <string>— usingstringrequires<string>(or transitive include via<iostream>). - Identifier starts with digit —
int 1count;won't compile. - Mismatched braces — every
{needs a}; missing one breaks the entire file.
- Sum two numbers — Read two integers with
cinand print their sum. Hint:int a, b; cin >> a >> b;. - Fix the bugs —
cout << Hello;(missing quotes) andcin << x;(wrong direction). Hint: strings need"...", cin uses>>. - Echo your name — Read a string
nameand printHello, NAME!. Hint: include<string>, declarestring name;. - Comment cleanup — Take any 10-line C++ snippet and add
//comments explaining each line. Hint: comments make code readable for the next person.
Think Like a Programmer: Syntax is the grammar of code. Get it wrong and the compiler refuses to talk to you. Get it right and the compiler becomes your friendliest teacher — its error messages will guide every fix.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Name? "; cin >> name;
cout << "Age? "; cin >> age;
cout << "Hello " << name << ", age " << age << endl;
return 0;
}