Linkers, Loaders & Executables
In C, what is the effect of defining a function with static (e.g. static int helper(void))?
- AThe function does not stay in memory during execution
- BThe function is visible only within its file and does not collide with a same-named function in another file
- CThe function runs before every other function
Show answer and explanation
Answer: B. The function is visible only within its file and does not collide with a same-named function in another file
Adding static to a function or global variable gives it internal linkage: the symbol is used only inside that file and cannot be referenced from other files. Besides preventing name collisions, it tells the compiler the function cannot be called from elsewhere, which makes optimizations such as inlining and removing unused functions easier.
- A. The function's code remains in memory throughout execution.
- B. Correct. It gets internal linkage.
- C. The order of execution does not change.