In what segment of an executable file are static variables stored so that they don’t have name collision? For example:
foo.c: bar.c:
static int foo = 1; static int foo = 10;
void fooTest() { void barTest() {
static int bar = 2; static int bar = 20;
foo++; foo++;
bar++; bar++;
printf("%d,%d", foo, bar); printf("%d, %d", foo, bar);
} }
If I compile both files and link it to a main that calls fooTest() and barTest repeatedly, the printf statements increment independently. Makes sense since the foo and bar variables are local to the translation unit.
But where is the storage allocated?
Honestly, the assumption is that you have a toolchain that would output a file in ELF format.In this manner, I accept that there must be some space held in the executable file for those static variables.
For conversation purposes, let’s accept we utilize the GCC toolchain.
Before trying this Thing I have also done several research on the web and read a couple of articles on the static variable in c to understand the concept in a better way, my source of knowledge: Wiki and scaler.
Thanks in advance!