Name spaces

From cppreference.com
< c‎ | language

If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates 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 any 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).

[edit] Notes

There is only one name space for tags even though three are possible. So, in practice, an identifier may appear multiple times without conflict as a label, as a variable, and as a tag of exactly one of the following: struct, union, or enum. Also, an identifier may appear in multiple occurrences of struct and union since each structure or union has a separate name space for its members.


[edit] Example

Identifiers test, i, and c appear multiple times without conflict in different name spaces.

struct test {
       int i;
       char c;
} t;
 
struct sss {
       int i;
       char c;
} s;
 
union uuu {
      int i;
      char c;
} u;
 
enum eee {
     i,
     c
} e;
 
int test;
 
int main(void)
{
    t.i = 0;
    u.i = 0;
test:
 
    return 0;
}

Possible output:

(none)