Question 16
Where are static variables allocated in the C memory layout?
Detailed Solution
Static variables (both local and global) are allocated in the static data segment for the entire program execution lifetime.
Where are static variables allocated in the C memory layout?
Static variables (both local and global) are allocated in the static data segment for the entire program execution lifetime.
What happens when a static local variable is initialized inside a function?
Static variables are initialized once at compile/load time and retain their values across function invocations.
What is the default storage class for local variables declared inside a C function block?
Local variables inside function blocks default to the 'auto' storage class unless specified otherwise.
What will be the output of the following program?
#include <stdio.h>
void count_calls(void) {
static int c = 0;
c++;
printf("%d ", c);
}
int main(void) {
count_calls();
count_calls();
count_calls();
return 0;
}Static local variable 'c' initializes to 0 once. First call sets c=1, second sets c=2, third sets c=3.
What is the default initial value of an uninitialized static variable in C?
Static and global variables are automatically zero-initialized by default.