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.
c gcc c99 linkage
Dietrich epp
source share