Correspondence of the relationship between declaration and definition

I am wondering if the following is a C fragment in which the definition fcannot repeat what the flink has static, true:

static int f(int);

int f(int x) { return x; }

Clang does not give any warnings. I read section 6.7.1 of the C11 standard without finding an answer to my question.

One could imagine more questions in the same vein, for example, t1.c and t2.c below, and it would be nice if the answer was general enough to apply to some of them, but I'm really interested about the first example above.

~ $ cat t1.c
static int f(int);

int f(int);

int f(int x) { return x; }
~ $ clang -c -std=c99 -pedantic t1.c
~ $ nm t1.o
warning: /Applications/Xcode.app/…/bin/nm: no name list
~ $ cat t2.c
int f(int);

static int f(int);

int f(int x) { return x; }
~ $ clang -c -std=c99 -pedantic t2.c
t2.c:3:12: error: static declaration of 'f' follows non-static declaration
static int f(int);
           ^
t2.c:1:5: note: previous declaration is here
int f(int);
    ^
1 error generated.
+4
source share
2 answers

Binding rules are a bit confusing, and they are different for functions and objects. In short, the following rules:

  • .
  • static .
  • extern , , .
  • , extern ( ).

, :

static int f(int); // Linkage of f is internal.

int f(int); // Same as next line.

extern int f(int); // Linkage as declared before, thus internal.

int f(int x) { return x; }

, , undefined (. C11 (n1570) 6.2.2 p7):

int f(int); // Same as if extern was given, no declaration visible,
            // so linkage is external.

static int f(int); // UB, already declared with external linkage.

int f(int x) { return x; } // Would be fine if either of the above
                           // declarations was removed.

C11 6.2.2. N1570:

(3) static, . 30)

(4) , extern , 31) , . , .

(5) , , ​​ extern. , .

30) static, ; . 6.7.1. 31) 6.2.1, .

+6

C11, 6.2.2, 7 undefined.

, undefined.

, ( - , ) .

C11, 6.2.1

1 ; ; , ; typedef name; ; ; . . . , , .

+5

All Articles