0decimal0
0decimal0

Reputation: 3984

Namespace lookup order in C

Is there an order of lookup in the namespaces namely, tag namespace and ordinary name space ? Consider the following code :

#include <stdio.h>

int main (void){

  typedef struct{ //This belongs to ordinary name space
    int min;
  } st;
  st myst;
  myst.min=6;
  struct myst{ // This belongs to tag name space
    int min;
  };
  myst.min=7;
  printf("%d\n%d\n",myst.min,myst.min);
  return 0;
}

Output

7

7

The compiler looks for the variable to print in the tag namespace first , I guess. I don't know if the lookup is even done for the same identifier in the ordinary namespace and if it is done I am clueless why it doesn't print it.

Upvotes: 2

Views: 114

Answers (2)

zwol
zwol

Reputation: 140748

There is no namespace lookup order in C. Only one namespace is ever considered for any particular identifier; it is determined by what kind of identifier is being looked up. Structure tags are one kind, with their own namespace; variable names are in the broader category of "ordinary identifiers", which have a separate namespace. There are other name spaces, too, but the compiler can always tell from context which one is relevant to any given identifier.

Thus, in your program, both uses of myst.min refer to the variable declared as st myst; and there isn't any second variable in the "tag" namespace (as you seem to have thought there would be).

You can see this for yourself by commenting out everything within main above myst.min = 7:

#include <stdio.h>

int main (void){
#if 0
  typedef struct{ //This belongs to ordinary name space
    int min;
  } st;
  st myst;
  myst.min=6;
#endif
  struct myst{ // This belongs to tag name space
    int min;
  };
  myst.min=7;
  printf("%d\n%d\n",myst.min,myst.min);
  return 0;
}

Attempting to compile this will produce a hard error:

test.c: In function ‘main’:
test.c:14:3: error: ‘myst’ undeclared (first use in this function)

What the struct myst declaration does is declare another type, which you can then use to declare variables. But it's not used for anything unless you actually do that. For instance

#include <stdio.h>

typedef struct { int min; } st;
struct myst { int min };
int main(void)
{
    // uses the typedef name 'st', in the ordinary namespace,
    // to declare the variable 'myst', also in the ordinary namespace
    st myst = { 6 };

    // uses the struct name 'myst', in the tag namespace,
    // to declare the variable 'myst2', in the ordinary namespace
    struct myst myst2 = { 7 };

    printf("%d %d\n", myst.min, myst2.min);
    return 0;
}

will print 6 7. This program also illustrates how myst, the variable, and myst, the struct tag, are indeed in two different namespaces and can be referred to independently.


(Thanks to John Bollinger for the new first paragraph. —ed)

Upvotes: 7

n. m. could be an AI
n. m. could be an AI

Reputation: 120011

C namespaces are completely disjoint. Each identifier is searches ony in one namespace. For example:

  • Identifiers that follow structand union keywords are searched in the tag namespace
  • Identifiers that follow the goto keyword are searched in the label namespace
  • Identifiers that follow . or -> symbols are searched in their structure or union member namespace (each structure and union has its own member namespace; type of the preceding expression determines which one to search)
  • Other identifiers are searched in the ordinary namespace.

There is no lookup order. The (unique) namespace to search is completely determined by the context.

Upvotes: 3

Related Questions