← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 8 of 15

C++ Keywords & Identifiers

Keywords

Keywords are words the C++ language itself reserves for specific purposes, like int, return, and class, and the compiler assigns them fixed meaning that can't be repurposed. Trying to name a variable int or class will fail to compile, because the compiler expects those words to appear only in their reserved role.

Example: Keywords

cpp
#include <iostream>

int main() {
	// 'int', 'return' are keywords -- reserved, fixed meaning
	int age = 25;
	return 0;
}

Identifiers

Identifiers are the names you choose for variables, functions, and classes, and picking clear ones is one of the cheapest ways to make code easier to read. A variable named totalPrice communicates its purpose instantly, whereas a name like x forces anyone reading the code to trace back through logic to understand it.

Example: Identifiers

cpp
#include <iostream>

int main() {
	int totalPrice = 100; // clear identifier vs an unclear name like x
	std::cout << totalPrice << std::endl;
	return 0;
}

Naming Rules

C++ requires identifiers to start with a letter or underscore (never a digit) and to contain only letters, digits, and underscores — no spaces, hyphens, or symbols like $ or #. These rules exist because the compiler needs an unambiguous way to tell where one identifier ends and the next token begins.

Example: Naming Rules

cpp
#include <iostream>

int main() {
	int _count = 0;   // starts with underscore: valid
	int count2 = 1;   // letters/digits only: valid
	std::cout << _count << " " << count2 << std::endl;
	return 0;
}

Case Sensitivity in Names

Because C++ is case-sensitive, count and Count are two entirely distinct identifiers as far as the compiler is concerned, even though they look nearly identical to a human. This is a common source of 'undefined reference' errors when a variable is declared with one casing and used with another by mistake.

Example: Case Sensitivity in Names

cpp
#include <iostream>

int main() {
	int count = 1;
	int Count = 2; // a distinct identifier from 'count'
	std::cout << count << " " << Count << std::endl;
	return 0;
}

Best Practices for Names

Favor descriptive, specific names over abbreviations or single letters, except for very short-lived loop counters like i where the convention is well understood. Consistent naming conventions across a codebase — like always using camelCase for variables — make it much easier to skim and understand unfamiliar code quickly.

Example: Best Practices for Names

cpp
#include <iostream>

int main() {
	for (int i = 0; i < 3; i++) { // short name OK for a loop counter
		int orderTotal = i * 10;    // descriptive name elsewhere
		std::cout << orderTotal << std::endl;
	}
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.