Identifiers
Identifiers are names that can refer to the following program entities:
objects
functions
structures, unions, and enumerations, along with their members
type aliases
labels
Every identifier has a name, belongs to a namespace, is restricted by its scope, and may be linked to other identifiers via linkage. Identifiers are introduced to the program through declarations and label statements.
Name
A valid identifier consists of any combination of upper-and-lower-case letters, digits, and underscores (‘_’) that does not begin with a digit. There are, however, some particular identifiers or patterns of identifiers that are off-limits, called reserved identifiers; these are the keywords listed in 6.4.1 Keywords and several classes of identifiers listed in 7.1.3 Reserved identifiers.
Some broad classes of identifiers are reserved, summarized below,
Identifiers beginning with an underscore followed by an upper-case letter or by a second underscore are always reserved.
File-scope identifiers beginning with an underscore are reserved at file-scope, but may be freely used in all other scopes.
Identifiers beginning with
to
,is
,str
,mem
, orwcs
, and followed by any lower-case letter are reserved.Identifiers beginning with
E
,LC_
,SIG
, orSIG_
, and followed by an upper-case letter are reserved.
Declaring reserved identifiers in a program can cause conflicts with the library and other aspects of the C implementation, leading to undefined program behavior.
Namespace
Each identifier is part of a specific namespace, and the namespace an identifier belongs to is determined by its immediate context,
- Label Names
The label namespace handles identifiers introduced in label statements or used as targets of the
goto
statement.- Tag Names
The tag namespace manages identifiers immediately following
struct
,union
, andenum
keywords, identifying specific structures, unions, or enumerations.- Member Names
Each structure and union has its own namespace for resolving member names when accessed through the
.
(dot) and->
(arrow) operators.- Ordinary Identifiers
All remaining identifiers, including object and function identifiers, type aliases, and enumeration constants, share the same namespace.
Note
Object oriented languages often have a concept of named namespaces, such as the std::
namespace in C++. In C, these do not exist; instead, the convention when writing third-party libraries is to prepend the library name to all identifiers, such as my_library_do_something_func()
, my_library_buffer_size
, etc.
Scope
Identifiers have an associated scope, which is the portion of the program where an identifier is visible and can be accessed:
- File Scope
Identifiers declared outside of any other scope have file scope which extends from the point of declaration to the end of the file.
- Block Scope
Identifiers declared inside a block or in the list of a function’s parameters have block scope that extends from the point of declaration to the closing brace (‘}’) of the associated block.
- Function Scope
Labels (and only labels) have function scope, and may be referred to from anywhere within the entire body of the function in which they appear–including before the label.
- Function Prototype Scope
Identifiers declared in a function prototype‘s parameter list remain in scope until the end of the prototype declaration.
Identifiers in inner scopes shadow identifiers declared in outer scopes with the same name to resolve naming conflicts.
Linkage
Identifiers declared more than once in different scopes or in the same scope can be made to refer to the same entity by a process called linkage. There are three types of linkage,
- No Linkage
Identifiers without linkage can only be referred to within the scope they appear in. Each declaration of an identifier with no linkage denotes a unique entity.
- Internal Linkage
Identifiers with internal linkage can be referred to within the entire translation unit. Each declaration of a particular identifier within the same translation unit denotes the same entity.
- External Linkage
Identifiers with external linkage can be referred to from any translation unit in the entire program. Each declaration of a particular identifier within the set of translation units and libraries that constitutes a program denotes the same entity.
Only objects can functions can have linkage