What Is an Identifier in C, C++ and C#?

JAVAScript

 

zokara / Getty Images

In C, C++, C# and other programming languages, an identifier is a name that is assigned by the user for a program element such as variable, type, template, class, function or namespace. It is usually limited to letters, digits, and underscores. Certain words, such as "new," "int" and "break," are reserved keywords and cannot be used as identifiers. Identifiers are used to identify a program element in the code. 

Computer languages have restrictions for which characters can appear in an identifier. For example, in early versions of the C and C++ languages, identifiers were restricted to a sequence of one or more ASCII letters, digits, which may not appear as the first character, and underscores. Later versions of these languages support almost all Unicode characters in an identifier with the exception of white space characters and language operators.

You designate an identifier by declaring it early in the code. Then, you can use that identifier later in the program to refer to the value you assigned to the identifier.

Rules for Identifiers

When naming an identifier, follow these established rules:

  • An identifier cannot be a C# keyword. Keywords have predefined special meanings to the compiler.
  • It cannot have two consecutive underscores.
  • It can be a combination of numbers, letters, connectors, and Unicode characters.
  • It must start with a letter of the alphabet or an underscore, not a number.
  • It should not include white space.
  • It cannot have more than 511 characters.
  • It has to be declared before it is referred.
  • Two identifiers cannot have the same name.
  • Identifiers are case sensitive.

For implementations of programming languages that are compiled, identifiers are often only compile-time entities. That is, at run time the compiled program contains references to memory addresses and offsets rather than the textual identifier tokens—these memory addresses or offsets having been assigned by the compiler to each identifier.

Verbatim Identifiers

Adding the prefix "@" to a keyword enables the keyword, which is normally reserved, to be used as an identifier, which can be useful when interfacing with other programming languages. The @ is not considered part of the identifier, so it might not be recognized in some languages. It is a special indicator to not treat what comes after it as a keyword, but rather as an identifier. This type of identifier is called a verbatim identifier. Using verbatim identifiers is allowed but strongly discouraged as a matter of style.

Format
mla apa chicago
Your Citation
Bolton, David. "What Is an Identifier in C, C++ and C#?" ThoughtCo, Aug. 25, 2020, thoughtco.com/definition-of-identifier-958092. Bolton, David. (2020, August 25). What Is an Identifier in C, C++ and C#? Retrieved from https://www.thoughtco.com/definition-of-identifier-958092 Bolton, David. "What Is an Identifier in C, C++ and C#?" ThoughtCo. https://www.thoughtco.com/definition-of-identifier-958092 (accessed April 19, 2024).