void testFunc(int);
int main(int argc, char** argv) {
testFunc(1);
testFunc(2);
testFunc(3);
return (EXIT_SUCCESS);
}
void testFunc(int another)
{
int num;
printf("num: %i\n", num);
num = another;
}
output:
num: 127383283
num: 1
num: 2
If I am printing the variable before I assign it to something each time, shouldn’t I always get garbage values without a static keyword?
You are getting garbage values – it just so happens that in this case those garbage values happen to be the value that you assigned in the previous invocation of the function.
If you call another function in between the calls to
testFunc(), or compile with higher optimisation settings, or with a different compiler, you’ll probably see something completely different.