Clarification of scope and revaluation

Referring to the following code:

#include <stdio.h>

int a;
int a;

int main()
{
    int b;
    int b;

    return 0;
}

Why does the compiler (GCC) complain about re-declaring only for variable "b" and not "a"?

redef.c: In the function "main": redef.c: 19: error: fixing "b" without binding

redef.c: 18: error: previous declaration 'b' was here

+4
source share
3 answers

This is because it ahas external communication and standard states (C11, 6.2.2 / 2):

An identifier declared in different areas or in the same area more than once can be made to refer to the same object or function in a process called a binding. There are three types of connections: external, internal, and none.

, , . . .

, a , . b , , , .

+8

C99 & sect; 6.9.2 & para; 2

static, . , , , , 0.

,

int a;
int a;

. , ,

int a = 0;

b, main, , .. . .

+1

int a; int a; . 6.9.2p2:

, , static, . , , , 0.

.

, int b; int b; , 6.7p3:

If the identifier has no binding, there should be no more than one identifier declaration (in the declarator or type specifier) ​​with the same scope and in the same space name

Identifiers declared inside a function, and not staticor externdo not have a binding, this is described in 6.2.2p6

The following identifiers are not associated: an identifier declared as something other than an object or function; identifier declared as a function parameter; block area identifier for an object declared without an extern storage class specifier.

0
source

Source: https://habr.com/ru/post/1536774/


All Articles