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;
}

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.

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

+5% cooler to you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.