Does the link in C: GCC follow the C99 specification, or do I not understand the specification?

I'm trying to understand the exact behavior of storage class specifiers in C99, and some GCC behavior doesn't seem to follow the spec if I misunderstood the spec. From clause 6.2.2 (2):

Within one translation unit, each identifier declaration with an internal link identifies the same object or function.

However, I tested GCC (powerpc-apple-darwin9-gcc-4.2.1) with the following program:

#include <stdio.h> static int f() { static int x = 0; return x++; } static int g() { static int x = 0; return x++; } int main(int argc, char *argv[]) { printf("g() = %i\n", g()); printf("g() = %i\n", g()); printf("f() = %i\n", f()); printf("f() = %i\n", f()); return 0; } 

Compiled with -std=c99 , it prints the following:

 g() = 0 g() = 1 f() = 0 f() = 1 

If I understand the specification correctly, it should print:

 g() = 0 g() = 1 f() = 2 f() = 3 

I understand why the GCC will deviate from the specification here, I am just wondering if there is a deeper explanation for this behavior.

+6
c gcc c99 linkage
source share
2 answers

The following paragraph 6.2.2 / 3 is important:

If the declaration of the file area identifier for an object or function contains a static storage class specifier, the identifier has an internal relationship.

(note the highlighted file area identifier).

Your static x variables do not have a file scope; they have a block scope.

+9
source share

6.2.2 (6) states:

The following identifiers have no binding: [...] block area identifier for an object declared without an extern storage class specifier.

Static variables are block area identifiers for objects, and they are not extern declared. Therefore, they have no connection, especially no internal connection.

+10
source share

All Articles