0

My understanding of the C language is that the same identifier cannot be reused. However, the code below compiles on GCC without complaints, even when using the "-Wall -pedantic" flags. Is there something that I am missing? Does the standard say anything about functions/macros having the same name as typedef'd types?

#include <stdio.h>

typedef int error;

#define init() error var_error = -6

#define error() ((const error)var_error)

int main(void)
{
    init();
    printf("error() = %d\n", error());

    return 0;
}
2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by _avishek
1

My understanding of the C language is that the same identifier cannot be reused.

That's correct, but only within the same name space (not the same as a C++ namespace). There are five listed name spaces in C:

  1. Label names (such as cases and goto labels).
  2. Structure, union, or enum tags.
  3. Structure or union members.
  4. Typedef names;
  5. Everything else.

With those name spaces in mind, it's easy to understand why the following is legal:

typedef struct foo { int foo; } foo;

In your code, the typedef error and the macro error are in different name spaces (the typedef falls under #4 and the macro under #5), so there's no direct conflict.

Edited by deceptikon

0

That's pretty neat! I did not know about these implied name spaces.

+5% cooler to you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.