In C,why is multiple declarations working fine for a global variable but not for a local variable?

后端 未结 3 1077
面向向阳花
面向向阳花 2020-11-30 09:12

In the following code, why do multiple declarations (and one definition) work fine for a global variable x but not for a local variable y which is

3条回答
  •  青春惊慌失措
    2020-11-30 09:40

    This is the way defined in the C99 standard, section 6.2.2, part 2:

    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

    The "global" variables x have external linkage, so they denote the same object. The local y variables, on the other hand, has no linkage, so there is a collision.

    References: C99 Standard.

提交回复
热议问题