C++ — Syntax
Statements end with semicolons
int x = 5;
std::cout << "Hello" << std::endl;
Missing semicolons cause compile errors. The error points to the next line.
Blocks use curly braces
int main() {
if (true) {
std::cout << "yes" << std::endl;
}
}
Every { needs a matching }. Use consistent indentation.
Namespaces
C++ uses namespaces to organize code and prevent naming conflicts:
#include <iostream>
namespace mylib {
void greet() {
std::cout << "Hello from mylib!" << std::endl;
}
}
int main() {
mylib::greet(); // explicit namespace
return 0;
}
The std namespace contains all standard library features. Use std:: prefix or using namespace std;.
Headers
#include <iostream> // input/output
#include <string> // string class
#include <vector> // dynamic arrays
#include <cmath> // math functions
#include <algorithm> // sort, find, etc.
#include <fstream> // file I/O
System headers use <>. Your own headers use "":
#include "myheader.h"
The main function
int main() {
// program starts here
return 0;
}
main() returns an integer — 0 means success. The OS uses this return value.
Variables and types
int age = 36; // integer
double pi = 3.14159; // double precision
float price = 9.99f; // single precision
char letter = 'A'; // single character
bool active = true; // boolean
std::string name = "Ada"; // string
auto x = 42; // type inferred
C++ is statically typed — the compiler knows every variable's type at compile time.
Constants
const int MAX_SIZE = 100;
const double PI = 3.14159;
constexpr int SQUARE_SIZE = 10;
// Old-style (avoid)
#define MAX_SIZE 100
const prevents modification. constexpr is computed at compile time.
References
int x = 5;
int& ref = x; // ref is an alias for x
ref = 10; // x is now 10
References are aliases — they don't copy data. Essential for functions that need to modify arguments.
Output
#include <iostream>
using namespace std;
int main() {
int age = 36;
string name = "Ada";
cout << "Hello, " << name << "!" << endl;
cout << "Age: " << age << endl;
cout << "Pi: " << 3.14159 << endl;
return 0;
}
cout chains values with <<. endl adds a newline and flushes the buffer.
Input
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter age: ";
cin >> age;
cout << "Hello, " << name << "! Age: " << age << endl;
return 0;
}
cin >> reads one value. getline(cin, var) reads a full line.
Comments
// Single-line comment
/*
Multi-line comment
*/
/// Documentation comment (for Doxygen)
Comments and code quality
The best C++ code reads clearly without comments:
// Bad — comments that restate the code
// increment counter
counter++;
// Good — comments that explain WHY
// Retry 3 times because the API is flaky
for (int i = 0; i < 3; i++) {
Common compile errors
// Missing semicolon
int x = 5 // error: expected ';' before '}' token
// Undefined reference
cout << "hi"; // error: 'cout' was not declared
// Fix: #include <iostream> and std::cout or using namespace std
// Missing closing brace
int main() {
cout << "hello";
// error: expected '}' at end of input
Mini Practice
- Create variables of each basic type and print them
- Use a namespace to organize your functions
- Use
autoto infer types in variable declarations - Create a reference to a variable and modify it through the reference
- Read a user's full name with
getlineand print it back
Next: output in C++ →
Related Topics
Frequently Asked Questions about Syntax
What is Syntax in C++?
Syntax is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Syntax?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.
Why is Syntax important in C++?
Syntax is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.