Namespaces of identifiers

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. Note that the term namespace as used here applies to C as well as C++ and does not refer to the C++ namespace language feature. 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.

You can redefine identifiers in the same namespace but within 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 name[20];  /* this structure member may not be named student      */
      int section;
      int id;
   } sam;             /* this structure variable should not be named student */

   goto student;
   student:;          /* null statement label            */
   return 0;

   student fred;      /* legal struct declaration in C++ */
}

The compiler interprets each occurrence of student by its context in the program. For example, when student appears after the keyword struct, it is a structure tag. The name student may not be used for a structure member of struct student. When student appears after the goto statement, the compiler passes control to the null statement label. In other contexts, the identifier student refers to the structure variable.