Every C++ program is built from a small set of foundational building blocks: keywords reserved by the language itself, identifiers chosen by the programmer to name things, and variables that store the actual data a program works with while it runs. Understanding how these three pieces fit together is one of the first real steps toward writing working C++ code.
Keywords are words the C++ language has reserved for its own use, each carrying a fixed, built-in meaning that cannot be changed or reused as the name of anything else. Words such as int, if, else, while, return, class, and const are all keywords. Because the compiler treats them as fixed instructions, none of them can be used as the name of a variable, function, or class – attempting to declare something called int or return, for instance, will always produce a compilation error.
An identifier is the name a programmer gives to a variable, function, class, or any other element they create in a program. Unlike keywords, identifiers are entirely up to the programmer to choose, within a few fixed rules: an identifier must start with a letter or an underscore, may contain letters, digits, and underscores after that, cannot contain spaces or symbols like @ or %, and cannot be identical to a reserved keyword.
int studentAge; // valid identifier
int _score; // valid identifier
int total2024; // valid identifier
int 2024total; // invalid - cannot start with a digit
int student age; // invalid - contains a space
Beyond what the compiler strictly requires, good identifier names are also meaningful to the humans reading the code – studentAge communicates intent far better than a name like x or a1, even though both compile equally well.
A variable is a named location in memory that holds a value which can change while the program runs. Declaring a variable requires specifying its data type and giving it an identifier as a name:
int age; // declares an integer variable named age
double price; // declares a floating-point variable named price
char grade; // declares a single-character variable named grade
bool isPassed; // declares a true/false variable named isPassed
A variable can also be given a starting value at the moment it is declared, called initialization, rather than being assigned separately afterward:
int age = 20;
double price = 49.99;
char grade = 'A';
bool isPassed = true;
These three related terms are often confused by beginners. Declaration is the act of introducing a variable’s name and type to the compiler for the first time. Initialization is giving that variable its first value, at the same moment it is declared. Assignment is giving a variable a new value at any point afterward, using the = operator, and can happen as many times as needed throughout a program’s execution:
int score; // declaration only
score = 85; // assignment, happening separately afterward
score = 90; // a second assignment, replacing the previous value
int total = 100; // declaration and initialization together
While C++ itself does not enforce a particular naming style beyond its basic rules, most C++ codebases follow shared conventions to keep code consistent and readable. Variable names are typically written in camelCase, starting with a lowercase letter and capitalizing the first letter of each subsequent word, as in studentAge or totalPrice. Constants are often written entirely in uppercase with underscores separating words, such as MAX_LIMIT. Following a consistent convention, whichever one a project chooses, makes code considerably easier for other programmers – and for the same programmer returning to it months later – to read at a glance.
Once you’re comfortable here, continue with our guide on Data Types in C++. Readers wanting the formal language specification can consult isocpp.org.
No Comments