4.3: Identifier Names
- Page ID
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:
|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
- 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.
- 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 Cubbi, cppreference.com
"Identifier Names" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...firstname.lastname@example.org) is licensed under CC BY 4.0