C++ course menu
C++ Syntax and Program Structure: Statements, Blocks, Namespaces and Headers
Learn the basic rules of C++ syntax: statements and semicolons, blocks and braces, case sensitivity, whitespace, identifiers, namespaces and std::, header files, and why to avoid 'using namespace std'.
On this page
Every language has grammar rules. C++'s rules are strict, but they are consistent, and the compiler tells you when you break one. This lesson covers the rules you will use in every program.
Statements end with a semicolon#
A statement is one instruction. In C++, every statement ends with ;:
#include <iostream>
int main() {
int apples = 5;
int oranges = 3;
std::cout << apples + oranges << '\n';
}8Line breaks do not end statements; semicolons do. That means one statement can span several lines, which is useful for long expressions:
#include <iostream>
int main() {
std::cout << "This statement "
<< "continues on "
<< "three lines.\n";
}This statement continues on three lines.Blocks use curly braces#
A block groups statements with { and }. Functions, if statements and loops all use blocks. Indentation is not required by the compiler, but it is essential for people reading your code. Use 4 spaces (or your editor's default) consistently:
#include <iostream>
int main() {
int temperature = 31;
if (temperature > 30) {
std::cout << "It's hot.\n";
std::cout << "Drink water.\n";
}
}It's hot.
Drink water.C++ is case-sensitive#
score, Score and SCORE are three different names. Keywords such as int, if and return are always lowercase.
Identifiers: naming rules#
Names you choose for variables and functions are called identifiers. They:
- may contain letters, digits and underscores (
_); - must not start with a digit (
2nd_placeis invalid); - must not be a keyword (
int,class,return, …).
Common styles are snake_case (total_price, used by the standard library) and camelCase (totalPrice). Pick one and stay consistent. Avoid names that start with an underscore followed by a capital letter, or that contain double underscores; those are reserved for the compiler.
Namespaces and std::#
A namespace groups related names so they do not clash with names in other code. Everything in the C++ standard library lives in the namespace std, so you write std::cout, std::string and std::vector. The :: is the scope resolution operator: "the cout inside std".
You can create your own namespaces too:
#include <iostream>
namespace shop {
double tax_rate = 0.18;
}
int main() {
std::cout << "Tax rate: " << shop::tax_rate << '\n';
}Tax rate: 0.18Should you write using namespace std?#
Many tutorials start with using namespace std; so they can write cout instead of std::cout. It works, but it pulls hundreds of names from the standard library into your code, which can clash with your own names. For example, the standard library has std::count, std::size and std::distance, which are also very natural names for variables or functions.
This course writes std:: explicitly, which is what most professional codebases do. If you want shorter names, bring in only what you use:
#include <iostream>
#include <string>
using std::cout;
using std::string;
int main() {
string city = "Pune";
cout << city << '\n';
}PuneNever put using namespace std; in a header file, because it then affects every file that includes that header.
Header files and #include#
#include copies the contents of a header into your file before compiling. Standard library headers use angle brackets and have no .h extension:
Include a header for everything you use, even if it happens to work without it on your compiler. Your own headers use quotes: #include "shop.h".
Whitespace#
Spaces, tabs and blank lines between tokens are mostly ignored, so these two lines are the same to the compiler:
int total=price*quantity;
int total = price * quantity;Always write the second one. Readable code is easier to fix.
Common mistakes#
Practice#
Practice exercise
Create a namespace called school containing an int called students set to 42. In main, print Students: 42 using it, without writing using namespace.
Show solution
#include <iostream>
namespace school {
int students = 42;
}
int main() {
std::cout << "Students: " << school::students << '\n';
}Students: 42Key takeaway#
Finished this lesson?