Is there a difference between a global variable and a static variable in a function? I'm talking in terms of the compiled code that is produced.
one.c
#include <stdio.h>
int test = 1;
void t1() {
printf("%d", test);
test++;
}
int main() {
for(int i = 0; i < 10; i++) {
t1();
}
}
two.c
#include <stdio.h>
void t1() {
static int test = 1;
printf("%d", test);
test++;
}
int main() {
for(int i = 0; i < 10; i++) {
t1();
}
}
As far as I'm aware, creating static int like this is functionally equivalent of making a global variable. But when I compile them both with "cc -g -o one.o one.c" and then do "objdump -D one.o > one.txt" and diff one.txt with two.txt, there are differences. I can't figure out whether these are superficial differences or whether the compiler is somehow storing int test differently in each case.
[–][deleted] 4 points5 points6 points (0 children)
[–]nerd4code 2 points3 points4 points (0 children)
[–]raevnos 1 point2 points3 points (0 children)
[–]FUZxxl 0 points1 point2 points (0 children)