I found some behavior with extern and static (internal) variables that I find quite weird.
Here is an example:
/* file a.c */
#include <stdio.h>
/* variable with static linkage */
static int x = 24;
int f() {
/* variable with extern linkage */
extern int x;
return x;
}
int main() {
printf("%d\n", f());
return 0;
}
-
/* file x.c */
/* define symbol `x` to be an `int` equal to 100 */
int x = 100;
I compile this program using:
$ cc a.c x.c -o a
I then run my program and get this output:
$ ./a
24
Why does this program output 24 and not 100?