Qualcomm Interview Question

Explain the keywords static and volatile in C

Interview Answers

Anonymous

Oct 27, 2024

The static keyword can be used within functions to preserve their value between function calls and to also restrict a variable's scope to within the file in which it's declared. Similarly, volatile is a keyword that can be used to ensure that a variable does not get cached or otherwise optimized because it can change at any time as a result of factors outside of the program itself. It is often used to set flags by the register as well.

Anonymous

May 20, 2022

A static variable will only have one chunk of memory associated with it, regardless of how many instances are created. The volatile keyword will force the compiler to not make any optimizations. Ex if a variable is declared as volatile and checked every iteration of a while loop, the compiler will check it every iteration, instead of only checking it the first time.

1