#include // Some of the identifier names below are valid, // some are invalid, some are C++ keywords, and // some are reserved for use by the implementation. // // For each identifier, determine whether it is: // // 1. a valid identifier you may use, // 2. a C++ keyword, // 3. an invalid identifier, or // 4. a reserved identifier. // // For each identifier that you should not use, // suggest a small change that would make it a // suitable identifier for a local variable. // // Do not worry yet about the meaning of 'int'. // That will be introduced in the next topic. // // First, examine the identifiers without compiling // the program. // // Next, compile the program and examine the error // messages. Notice that the compiler can diagnose // keywords and syntactically invalid identifiers, // but it may not warn you about identifiers that // are syntactically valid but reserved. // // Finally, scroll down to compare your answers // with our comments. // Function declaration int main(); // Function definition int main() { int n; int double; int pi2; int 2pi; int n0; int return; int return_value; int an_identifier; int another__identifier; int *yet_another_identifier*; int _; int AnInvalidIdentifier; int static_assert; int 0_10_interval; std::cout << "Your code compiled and executed." << std::endl; return 0; } /* We will comment on each identifier in turn. int n; Valid. A single letter is perfectly valid as an identifier, although descriptive names are generally preferable except for conventional uses such as counters or mathematical variables. int double; Invalid for your use: 'double' is a C++ keyword. A keyword has a meaning defined by the language and cannot be used as an identifier. Depending on what the variable represents, alternatives might include double_value measurement value int pi2; Valid. Digits may appear in an identifier provided that the first character is not a digit. int 2pi; Invalid. An identifier cannot begin with a digit. Possible alternatives include two_pi pi_2 int n0; Valid. Again, digits are permitted after the first character. int return; Invalid for your use: 'return' is a C++ keyword. A possible alternative is return_value int return_value; Valid. int an_identifier; Valid. The underscore may appear within an identifier. int another__identifier; This has the lexical form of an identifier, but you must not use it. Any identifier containing two consecutive underscores __ anywhere in its name is reserved to the C++ implementation. A suitable alternative is another_identifier int *yet_another_identifier*; Valid for a local variable. This is an important subtlety. An identifier beginning with a single underscore followed by a lowercase letter is permitted as a local variable. Nevertheless, beginning your own identifiers with an underscore is generally best avoided, because other underscore rules are easy to confuse with this one. A clearer choice would be yet_another_identifier int _; Valid for a local variable. A single underscore is not reserved in this context. It is nevertheless usually a poor choice for a variable name because it communicates essentially nothing about what the variable represents. int AnInvalidIdentifier; Despite its name, this is a valid identifier. C++ identifiers are case-sensitive, so value Value VALUE are three different identifiers. int static_assert; Invalid for your use: 'static_assert' is a C++ keyword. You do not need to memorize every C++ keyword immediately. You will become familiar with the commonly used keywords as you encounter them. int 0_10_interval; Invalid. An identifier cannot begin with a digit. A suitable alternative is interval_0_10 ## A useful summary of the underscore rules For the kinds of identifiers you will normally create in this course, avoid: 1. any identifier containing '__', such as some__name 2. any identifier beginning with an underscore followed by an uppercase letter, such as _Value Both forms are reserved to the implementation. In addition, identifiers beginning with a single underscore are reserved in the global namespace. Thus, even though a name such as _value is permitted for a local variable, the simplest rule for your own code is: Do not begin your identifiers with underscores. This is a style rule rather than a complete statement of the C++ language rules, but it keeps you safely away from reserved identifiers. */