Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 03 / 714%· free preview
Getting Started3/5

C++ Syntax

Learn the rules of writing C++ — statements, blocks, comments, semicolons, and the structure every program follows.

C++ Program Skeleton

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
}
Statements & Semicolons

A statement is one instruction. Every C++ statement ends with a semicolon ;.

cpp
int x = 10;          // statement 1
cout << x << endl;   // statement 2

Forgetting ; is the #1 beginner error — expected ';' before '}'.

Blocks (Compound Statements)

Code surrounded by { } is a block. Functions, loops, ifs, classes — all use blocks.

cpp
if (x > 0) {
    cout << "positive";
    cout << " number";
}
Comments

Two flavours:

cpp
// Single-line comment

/* Multi-line
   comment */

Comments are ignored by the compiler. Use them to explain why, not what.

Case Sensitivity

C++ is case-sensitive:

cpp
int Age = 21;
cout << age;  // ERROR — 'age' not declared (it's 'Age')
Naming Rules (Identifiers)
  • 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.
Reserved Keywords (selection)

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.

Output: `cout`
cpp
cout << "Hello";              // print text
cout << 42;                   // print number
cout << "Sum = " << 10+5;     // chain
cout << endl;                  // newline + flush
cout << "\n";                  // newline (faster)
Input: `cin`
cpp
int age;
cout << "Enter age: ";
cin >> age;                    // read integer
cout << "You are " << age;
A Complete Example
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.
Common Mistakes
  • Missing semicoloncout << "Hi" without ; triggers a confusing compiler error on the next line.
  • Wrong arrow directioncin << age; (wrong, should be cin >> age).
  • Reading without #include <string> — using string requires <string> (or transitive include via <iostream>).
  • Identifier starts with digitint 1count; won't compile.
  • Mismatched braces — every { needs a }; missing one breaks the entire file.
Interview Questions

Practice Exercises
  1. Sum two numbers — Read two integers with cin and print their sum. Hint: int a, b; cin >> a >> b;.
  2. Fix the bugscout << Hello; (missing quotes) and cin << x; (wrong direction). Hint: strings need "...", cin uses >>.
  3. Echo your name — Read a string name and print Hello, NAME!. Hint: include <string>, declare string name;.
  4. 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.

AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

C++
#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;
}
Ready to move on?
// example library
Want more hands-on snippets in C++?
Browse 100 runnable examples · across 10 chapters · short, copy-paste-friendly · grouped by topic
Explore examples
// glossary lookup
Every term you just saw, explained
Quick definitions for variables, pointers, loops, functions and every concept in one searchable page.
Open Terminology
// feedback.matters()
Did this lesson help you?