
Identifiers in C++
This is a guide on identifiers in C++.
Check out Audible on Amazon and listen to the newest books!
When writing a C++ program, one of the first and most important tasks you will encounter is creating and using identifiers. An identifier is simply any name that you define within your code, and these names serve as the labels for the various elements that make up a program, including variables, functions, and classes. Because identifiers are the primary means by which you refer to the data and behavior in your program, understanding the rules that govern them—and the conventions that make them effective—is essential for writing code that is correct, readable, and maintainable.
Every identifier you create must follow a specific set of rules. First, each identifier must begin with a letter or an underscore. After that initial character, you are free to use any combination of letters, underscores, and digits. These rules are enforced by the compiler, so violating them will result in a compilation error. Beyond these hard requirements, however, there are important conventions that experienced programmers follow to make their code easier to work with.
One critical consideration is that C++ is a case-sensitive language. This means that uppercase and lowercase letters are treated as distinct characters. If, for example, you define an identifier named subtotal, you cannot later refer to it as Subtotal or SUBTOTAL. Doing so is a common coding error, and it can lead to confusing compiler messages or unexpected behavior. For this reason, you should be careful and consistent when you create and use identifiers throughout your program.
When you create an identifier, you should strive to make the name both meaningful and easy to remember. To make a name meaningful, use as many characters as you need so that other programmers can easily read and understand your code. A descriptive name communicates intent far better than a cryptic one, and the extra typing required is more than justified by the time you will save when you test, debug, and maintain the program. To make a name easy to remember, you should avoid abbreviations and choose words that are clear and easy for programmers to understand.
There are several widely used conventions for forming multi-word identifiers. One common approach is to separate two or more lowercase words with underscores, as in subtotal_value. This style is often referred to as "snake case," and many programmers use it. Another popular convention is to capitalize the first letter of each word, as in SubtotalValue. This style is called "camel case," and some developers prefer it. Whichever convention you choose, consistency within a project is important, and you should be prepared to follow the conventions established by the codebase you are working in.
Although meaningful names are generally preferred, programmers often use just one or two lowercase letters for certain common identifiers. For example, the letter i is frequently used to identify an integer that serves as a counter variable, particularly in loops. Such short names are acceptable when their meaning is obvious from context, but they should be used sparingly and only where clarity is not sacrificed.
Start each identifier with a letter or an underscore, followed by any combination of letters, underscores, and digits.
Remember that C++ is case sensitive, so
subtotalandSubtotalare different identifiers.Choose names that are meaningful and easy to remember, using as many characters as necessary for clarity.
Avoid abbreviations that could make your code harder to understand.
Follow a consistent naming convention, such as snake case or camel case.
Use short names like
ionly for common, contextually obvious identifiers such as loop counters.Avoid creating identifiers that match C++ keywords, which are reserved by the language.
For compatibility with C programs, also avoid identifiers that match C keywords.
When you create identifiers, you must not use any of the C++ keywords. These keywords are reserved by the C++ language for its own use, and the compiler will reject any attempt to use them as identifiers. To help you recognize keywords in your code, most development environments display them in a different color than the rest of your code, which makes them easy to spot. In addition to avoiding C++ keywords, you should also avoid creating identifiers that match any of the C keywords. This practice ensures compatibility with programs written in the C language. Most C keywords are the same as C++ keywords, so this is not difficult to do. However, there are a few keywords that are specific to C. A C++ compiler might allow you to use one of these C keywords as an identifier, but you should not do so, because doing so can cause problems if the code is ever compiled or integrated in a C context.
To summarize the distinction: an identifier is any name that you create in C++, whether for a variable, a function, or a class, while a keyword is a word that is reserved by the C++ language and therefore cannot be used as an identifier. For backward compatibility, you should also avoid C keywords. When you refer to an identifier, be sure to use the correct uppercase and lowercase letters, because C++ is a case-sensitive language. Following these rules and conventions will help you write code that compiles correctly, communicates clearly to other programmers, and remains easy to maintain over time.