C++ reserves a set of 63 words for it’s own use. These words are called keywords, and each of these keywords has a special meaning with in the C++ language. The 15 keywords that are starred (*) were added to the language after it’s initial release, consequently, some older reference books or material may omit these.
Here is a list of all the C++ keywords:
You have already run across some of these keywords, including int, void, return, using, and namespace. Along with a set of operators, these keywords define the entire language of C++ (preprocessor commands excluded). Because these keywords have special meaning, your IDEs will change the text color of these words (usually to blue) to make them more visible.
By the time you are done with this tutorial, you will understand what almost all of these words do!
Identifiers, and naming them
The name of a variable, function, class, or other entity in C++ is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:
- The identifier can not be a keyword. Keywords are reserved.
- The identifier can only be composed of letters, numbers, and the underscore character. That means the name can not contains symbols (except the underscore) nor whitespace.
- The identifier must begin with a letter or an underscore. It can not start with a number.
- C++ distinguishes between lower and upper case letters.
nvalueis different than
nValueis different than
Not too difficult, eh?
Now that you know how you can name a variable, let’s talk about how you should name a variable.
First, it is a convention that variable (and function) names begin with a lower case letter. If the variable or function name is one word, the whole thing should be written in lower case letters.
int value; // correct int Value; // incorrect (should start with lower case letter) int VALUE; // incorrect (should start with lower case letter) int VaLuE; // incorrect (see your psychiatrist) ;)
If the identifier is a multiword name, there are two common conventions: separated by underscores, or intercapped.
int my_variable_name; // correct (separated by underscores) int myVariableName; // correct (intercapped) int my variable name; // incorrect (spaces not allowed) int MyVariableName; // incorrect (should start with lower case letter)
In this tutorial, we will use the intercapped approach because it’s easier to read (it’s easy to mistake an underscore for a space in dense blocks of code).
Second, and this is perhaps the most important rule of all, give your identifiers names that actually describe what they are. It is typical for inexperienced programmers to make variable names as short as possible, either to save on typing or because they figure the meaning is obvious. This is almost always a mistake. Ideally, variables should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense. The more complex the code the variable is being used in, the better name it needs to have.
|int ccount||Bad||Nobody knows what a ccount is|
|int customerCount||Good||Clear what we’re counting|
|int i||Bad||What does I stand for?*|
|int index||Good||This variable is indexing something|
|int _count||Bad||Do not start variable names with underscore|
|int count||Either||Okay only if obvious what we’re counting|
|int data||Bad||What kind of data?|
|int value1, value2||Either||Can be hard to differentiate between the two|
|int x, y||Either||Okay only in trivial mathematical functions|
* Note: it is okay to use trivial variable names for variables that have a trivial use, such as loop variables. We will address this topic in the section on flow control.
Third, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfChars that is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variable numberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:
// holds number of chars in a piece of text -- including whitespace and punctuation! int numberOfChars;
One method that is sometimes used in naming variables is called Hungarian notation. In Hungarian notation, variables always begin with a prefix that indicates the variable’s type.
int nValue; // the n before Value represents that this is an integer bool bValue; // b means boolean char chValue; // ch means char double dValue; // d means double float fValue; // f means float
We will talk more about Hungarian notation later in this section once we’ve covered the different numerical data types.
Pick which variables are improperly declared and named according to standard conventions (ie. how you should name a variable).
1) int sum;
2) int _apples;
3) int cats=5, dogs=5;
4) int VALUE;
5) int clouds=5, int trees;
6) int my variable name;
7) int length=3; int width=4;
8) int TotalCustomers;
9) int void = 5;
10) int nAngle;
11) int 3some;
12) int meters_of_pipe;
13) int length, width=5;
|2.3 — Variable sizes and the sizeof operator|
|2.1 — Basic addressing and variable declaration|