C++ Keywords Reference

A keyword is a word that is reserved by the C++ language itself, such as int, if, or class. Keywords have a fixed meaning to the compiler, so you cannot use them as names for variables, functions, or classes.

Why keywords matter

You have already met many keywords in earlier lessons without necessarily calling them that — int, return, if, and for are all keywords. This lesson collects the most commonly used ones in one place so you have a quick reference to come back to.

Common keyword categories

Types

These keywords name or modify built-in data types.

Keyword Meaning
int Whole number type
double Floating-point number type
char Single character type
bool Boolean type (true or false)
void Represents “no value”, used for functions that return nothing
const Marks a value as unable to change after initialization
unsigned Restricts an integer type to non-negative values

Control flow

Keyword Meaning
if / else Branch based on a condition
for Loop a known number of times
while Loop while a condition stays true
do Loop body runs at least once, checked at the end
switch / case Branch on one of several fixed values
break Exit a loop or switch early
continue Skip to the next loop iteration
return Exit a function, optionally sending back a value

Classes and object-oriented code

Keyword Meaning
class / struct Define a new type made of data and functions
public Members accessible from outside the class
private Members accessible only inside the class
protected Members accessible in the class and its derived classes
static A member shared by all objects of a class, rather than one per object
this Pointer to the current object inside a member function
virtual Allows a derived class to override a function
new / delete Allocate and free memory on the heap

A few more useful ones

Keyword Meaning
namespace Groups names together to avoid naming clashes
using Brings a name into the current scope, e.g. using namespace std;
true / false The two boolean literal values
sizeof Gives the size in bytes of a type or variable
enum Defines a type with a fixed set of named values
auto Lets the compiler infer a variable’s type from its initializer

This is not the full list — C++ has around 90 keywords in total, including newer ones like nullptr and constexpr — but the tables above cover the ones you will use in almost every program.

Example

The program below uses several keywords from the tables above: class, public, static, const, void, for, if, else, and return.

#include <iostream>
using namespace std;

class Counter {
public:
    static const int max_value = 5;

    void run() {
        for (int i = 0; i < max_value; i++) {
            if (i % 2 == 0) {
                cout << i << " is even" << endl;
            } else {
                cout << i << " is odd" << endl;
            }
        }
    }
};

int main(void) {
    Counter c;
    c.run();
    return 0;
}

Output:

0 is even
1 is odd
2 is even
3 is odd
4 is even

How it works

static const int max_value belongs to the Counter class itself rather than to any one object, and const guarantees it cannot be changed. The public keyword makes run() callable from outside the class. Inside run(), the for loop uses if/else to decide what to print, and main uses return 0; to signal the program finished successfully.

Keep this page bookmarked as a quick lookup — the next lesson moves on to C++ operators.