Name spaces of identifiers

At a particular point in the source it is possible to have the same identifier declared and visible as a label, object/ function/ typedef/ enumeration constant, enum/ structure/ union tag, and a potentially infinite number of member names. Syntactic context in C is much more localized than in human languages. When it happens, we say that the name is overloaded and the context in which the name is used determines its association that is in effect. For instance an Identifier might be both the name of the variable and a structure tag. When used in an expression, the variable association is used; when used as type specifier, the tag association is used. The International Standard of Language C defines the name space as under:
If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguate’s uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows:
  • label names (disambiguated by the syntax of the label declaration and use);
  • the tags of structures, unions, and enumerations (disambiguated by following any32) of the keywords struct, union, or enum);
  • the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);
  • all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).
Namespaces are the various syntactic contexts within which an identifier can be used. Within the same context and the same scope, an identifier must uniquely identify an entity. The compiler sets up namespaces to distinguish among identifiers referring to different kinds of entities. Identical identifiers in different namespaces do not interfere with each other, even if they are in the same scope. The same identifier can declare different objects as long as each identifier is unique within its namespace. The syntactic context of an identifier within a program lets the compiler resolve its namespace without ambiguity. Within each of the following four namespaces, the identifiers must be unique:
  • Tags of these types must be unique within a single scope:
    • Enumerations
    • Structures and unions
  • Members of structures, unions, and classes must be unique within a single structure, union, or class type.
  • Statement labels have function scope and must be unique within a function.
  • All other ordinary identifiers must be unique within a single scope:
    • C function names (C++ function names can be overloaded)
    • Variable names
    • Names of function parameters
    • Enumeration constants
    • typedef names.
You can redefine identifiers in the same namespace using enclosed program blocks.Structure tags, structure members, variable names, and statement labels are in four different namespaces. No name conflict occurs among the items named student in the following example:
int get_item(){
    struct student{           /* structure tag
    char student[20];         /* structure member */
    int section;
    int id;
} student;                    /* structure variable */
goto student;
student:;                    /* null statement label *
return 0;
The compiler interprets each occurrence of student by its context in the program: when student appears after the keyword struct, it is a structure tag; when it appears in the block defining the student type, it is a structure member variable; when it appears at the end of the structure definition, it declares a structure variable; and when it appears after the goto statement, it is a label.
In C each name space is separate from other name spaces. As the name suggests this concept can be viewed as a space within which names exist. This specification was introduced in C90 so that existing code was not broken. There is also a name space for macro names. The standard specifies various identifiers, having particular spellings, as being reserved for various purposes. The term reserved name space is sometimes used to denote identifiers belonging to this set. However, this term does not occur in the C Standard; it is an informal term used by the committee and some developers.
Labels represent statement locations within a function body. Putting label names in their own name space represents a language design decision. It enables uses such as if (panic) goto panic.
A tag name always appears to the right of a keyword. Its status as a tag is clearly visible to the reader.. The original definition of C did not include support for typedef names. Tags were the mechanism by which structure, union, and enumerated types could be given a name that could be referred to later. Putting tags in a different name space removed the possibility of a tag name clashing with an ordinary identifier. It also has the advantage on large projects of reducing the number of possible naming conflicts, and allowing declarations such as the following to be used:
typedef struct listitem listitem;
Although each structure or union type definition creates its own unique name space, no new scope is created. The declarations contained within these definitions have the scope that exists at the point where the structure or union is defined. Members in different structure types may hold the same kind of information; for instance, a member named next might always point to the next element in a linked list, its type being a pointer to the structure type containing its definition. Members named x_coord and y_coord might occur in several structure types dealing with coordinate systems. The underlying rationale for the guideline recommendation dealing with reusing the same identifier name, confusion cased by different semantic associations, is only applicable if the semantic associations are different. If they are the same, then there is a benefit in reusing the same name when dealing with different members that are used for the same purpose.
Tagged , , , , , ,
%d bloggers like this: