Skip to main content
Engineering LibreTexts

4.3: Identifier Names

  • Page ID
    29031
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Identifier Names

    Within programming a variety of items are given descriptive names to make the code more meaningful to us as humans. These names are called "Identifier Names". Constants, variables, type definitions, functions, etc. when declared or defined are identified by a name. These names follow a set of rules that are imposed by:

    • the language's technical limitations
    • good programming practices

    Technical to Language

    An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (see below for details). A valid identifier must begin with a non-digit character. Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant.

    There are certain rules that should be followed while naming c identifiers:

    • They must begin with a letter or underscore(_).
      • the identifiers with a double underscore anywhere are reserved;
      • the identifiers that begin with an underscore followed by an uppercase letter are reserved;
      • the identifiers that begin with an underscore are reserved in the global namespace.
    • They must consist of only letters, digits, or underscore. No other special character is allowed.
    • It should not be a keyword.
    • It must not contain white space.
    • It should be up to 31 characters long as only first 31 characters are significant.

    Some examples of C++ identifiers:

    NAME REMARK
    _A9 Valid
    Temp.var Invalid as it contains special character other than the underscore
    void Invalid as it is a keyword

    These attributes are specific to C++ - the requirements vary from one programming language to another. The allowable characters and reserved words will be different. The length limit refers to how many characters are allowed in an identifier name and often is compiler dependent and may vary from compiler to compiler for the same language. However, all programming languages have these three technical rules.

    Good Programming Techniques

    • Meaningful
    • Be case consistent - making sure you are coding according to the standards your organization has in place
    • CONSTANTS IN ALL UPPER CASE - not a requirements - but pretty much an industry standard concept

    Meaningful identifier names make your code easier for another to understand. After all what does "p" mean? Is it pi, price, pennies, etc. Thus do not use cryptic (look it up in the dictionary) identifier names. This has been discussed previously - you will lose points in my class if you use variables that are NOT meaningful.

    Some programming languages treat upper and lower case letters used in identifier names as the same. Be sure you know the coding requirements for whatever organization you are coding for.

    Definitions

    reserved word
    Words that cannot be used by the programmer as identifier names because they already have a specific meaning within the programming language.

    Adapted from: "C/C++ Tokens" by I.HARISH KUMAR, Geeks for Geeks
    "Identifiers" by Cubbicppreference.com
    "Identifier Names" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2) is licensed under CC BY 4.0


    This page titled 4.3: Identifier Names is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.