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 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.



[ Top of Page | Previous Page | Next Page | Contents | Index ]