Some Name
Some Name

Reputation: 9540

Namespaces in C formal definition

I'm reading the N1570 Standard and have a problem to understand the wording of the name space definition. Here is it:

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

32) There is only one name space for tags even though three are possible.

Here they are talking about in case of more than 1 declaration of particular identifiers is visible. Now words something like "To access an identifier one shall specify its namespace" or "To access an identifier in a specific namespace...".

Upvotes: 2

Views: 86

Answers (1)

Sourav Ghosh
Sourav Ghosh

Reputation: 134406

Let me show an example first (this is strictly for understanding purpose, dont write code like this, ever)

#include  <stdio.h>


int main(void)
{
    int here = 0;    //.......................ordinary identifier
    struct here {    //.......................structure tag
        int here;    //.......................member of a structure
    } there;

here:                             //......... a label name
     here++;
     printf("Inside here\n");
     there.here = here;           //...........no conflict, both are in separate namespace
     if (here > 2) {
         return 0; 
     }
     else
       goto here;                 //......... a label name

    printf("Hello, world!\n");          // control does not reach here..intentionally :)
    return 0;
}

You see usage of identifier here. They belong to separate namespace(s) according to the rule, hence this program is fine.

However, say, for example, you change the structure variable name, from there to here, and you'll see a conflict, as then, there would be two separate declaration of same identifier (ordinary identifier) in the same namespace.

Upvotes: 7

Related Questions