Why are typedef identifiers allowed to be declared multiple times?

后端 未结 3 1162
醉梦人生
醉梦人生 2020-12-05 15:41

From the C99 standard, 6.7(5):

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a d

3条回答
  •  無奈伤痛
    2020-12-05 16:09

    For the same reason that other declarations are allowed to be declared multiple times. For example:

    void foo(int a);
    
    void foo(int a);
    
    int main()
    {
        foo(42);
    }
    
    void foo(int a)
    {
        printf("%d\n", a);
    }
    

    This allows more than one header to declare a function or structure, and allow two or more such headers to be included in the same translation unit.

    typedefs are similar to prototyping a function or structure -- they declare identifiers that have some compile time meaning, but no runtime meaning. For example, the following wouldn't compile:

    int main()
    {
        typedef int x;
        typedef int x;
        x = 42;
    }
    

    because x does not name a variable; it is only a compile time alias for the name int.

提交回复
热议问题