Printing and Variables
C++ programs are built from #include directives, a main() function, and statements inside it -- every statement ends with a semicolon.
Your first program
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
Variables
Every variable's type must be declared before it's used:
#include <string>
int age;
double gpa;
string name;
Assigning values
int age = 20;
double gpa = 4.5;
string name = "Ada";
= is the assignment operator here -- == is only for comparisons.
Getting input
#include <iostream>
using namespace std;
int main() {
string name;
cout << "What is your name? ";
cin >> name;
cout << "Hello, " << name << endl;
return 0;
}
Comments
// This is a comment -- C++ ignores everything after //
/* This is a
multi-line comment */
Why This Matters for Your Exams
C++ is strongly, statically typed -- unlike Python, every variable's type is fixed the moment it's declared and checked before the program even compiles. Forgetting a semicolon or mismatching a type is the single most common beginner compile error.
Try it yourself
Change the name below and press Run to see C++'s own output.
lock
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for CPP.

