2.2.3. Identifiers

Identifiers in C++ can be composed of letters, digits, and the underscore character. The language imposes no limit on name length. Identifiers must begin with either a letter or an underscore. Identifiers are case-sensitive; upper- and lowercase letters are distinct:

// defines four different int variables
int somename, someName, SomeName, SOMENAME;

The language reserves a set of names, listed in Tables 2.3 and Table 2.4, for its own use. These names may not be used as identifiers.

Table 2.3. C++ Keywords

Image

Table 2.4. C++ Alternative Operator Names

Image

The standard also reserves a set of names for use in the standard library. The identifiers we define in our own programs may not contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an uppercase letter. In addition, identifiers defined outside a function may not begin with an underscore.

Conventions for Variable Names

There are a number of generally accepted conventions for naming variables. Following these conventions can improve the readability of a program.

• An identifier should give some indication of its meaning.

• Variable names normally are lowercase—index, not Index or INDEX.

• Like Sales_item, classes we define usually begin with an uppercase letter.

• Identifiers with multiple words should visually distinguish each word, for example, student_loan or studentLoan, not studentloan.


Image Best Practices

Naming conventions are most useful when followed consistently.



Exercises Section 2.2.3

Exercise 2.12: Which, if any, of the following names are invalid?

(a) int double = 3.14;

(b) int _;

(c) int catch-22;

(d) int 1_or_2 = 1;

(e) double Double = 3.14;


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset