Linkers, Loaders & Executables
In C, what is true of the starting values of a local variable and a global variable declared without initializers?
- ABoth always start at 0
- BThe local starts at 0, but the global is indeterminate
- CThe global starts at 0, but the local's value is indeterminate
Show answer and explanation
Answer: C. The global starts at 0, but the local's value is indeterminate
Global and static variables live in .bss, which is zero-filled when the program starts, so they begin at 0. Local variables are created on the stack each time the function is called; clearing them every time would cost time, so leftovers from earlier use of that memory may remain. Reading an uninitialized local can give results that change from run to run, so locals should always be initialized before use.
- A. Locals are not cleared, so they are indeterminate.
- B. It is the other way around.
- C. Correct. It follows from where each is placed.