C++ Identifiers

C++ identifiers are the name assigned to a function, variable, or any other user-defined item. They are the basic requirement of any language. Identifiers can be from one to several characters long.

In short, we can say that the C++ identifiers represent the essential elements in a program which are given below:

  • Constants
  • Variables
  • Functions
  • Labels
  • Defined data types
Identifier Naming Rules

Some naming rules are common in both C and C++. They are as follows:

  • Variable names can start with any letter of the alphabet or an underscore.
  • The identifier name cannot start with a digit, i.e., the first letter should be alphabetical. After the first letter, we can use letters, digits, or underscores. The underscore can be used to enhance the readability of a variable name, as in line_count.
  • Uppercase and lowercase are seen as different; that is, to C++, myvar and MyVar are separate names. So, we can say that C++ identifiers are case-sensitive.
  • A declared keyword cannot be used as a variable name. In addition, predefined identifiers such as cout are also off limits.
Guidelines for Identifier Names

Below are some guidelines for working with identifiers

  • Identifier names should be long enough to be descriptive.
  • The length of a variable’s name should be proportional to its scope.
  • Avoid cryptic abbreviations and give proper spelling for identifiers.
  • Short names (x, b, y, and so on) should be used only where their brevity makes the code more readable and where the usage is so obvious that a descriptive name is not needed.
  • Make sure identifiers look and sound different from one another to minimize confusion.
  • Function (or method) names should be usually like verbs or verb-noun phrases. For example search(), reset(), findParagraph(), showCursor().
  • Variable names are usually abstract nouns, possibly with an additional noun: count, state, windSpeed, windowHeight.
  • Boolean variables should be named appropriately. Eg: windowIconized, fileIsOpen.
Spelling and Capitalization of Names

When creating your own style spelling and capitalization should not be overlooked .

Below are some tips for these areas include the following.

  • Identifiers should be consistent and use mixed case where appropriate.
  • Function names, class, typedef, struct names, data members and locals should begin with a lowercase letter (often called camel case, as in myVariable).
  • Enumerated constants should begin with a few lowercase letters as an abbreviation for the enum.
  • Example
    snippet
    enum TextStyle
    {
    tsPlain,
    tsBold,
    tsItalic,
    tsUnderscore,
    };
    Valid/ Invalid Identifiers
    Example #1

    Valid Identifiers

    The following are the examples of valid identifiers are:

    snippet
    Result
    Test2
    _sum
    power
    Example #2

    Invalid Identifiers

    The following are the examples of invalid identifiers.

    snippet
    Sum-1   // containing special character '-'.
    2data    // the first letter is a digit. 
    break    // use of a keyword.

    Below table shows the valid and invalid identifiers.

    ValidInvalid
    Count 1count
    test23hi!there
    high_balancehigh...balance

    The major difference between C and C++ is the limit on the length of the name of the variable. ANSI C considers only the first 32 characters in a name while ANSI C++ imposes no limit on the length of the name.

    Differences between Identifiers and Keywords

    The following is the list of differences between identifiers and keywords.

    Identifiers Keywords
    Identifiers are the names defined by the programmer to the basic elements of a program. Keywords are the reserved words whose meaning is known by the compiler.
    It is used to identify the name of the variable. It is used to specify the type of entity.
    It can consist of letters, digits, and underscore. It contains only letters.
    It can use both lowercase and uppercase letters. It uses only lowercase letters.
    No special character can be used except the underscore. It cannot contain any special character.
    The starting letter of identifiers can be lowercase, uppercase or underscore. It can be started only with the lowercase letter.
    It can be classified as internal and external identifiers. It cannot be further classified.
    Examples: test, result, sum, power, etc. Examples: 'for', 'if', 'else', 'break', etc.
    Related Tutorial
    Follow Us
    https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
    Contents +