C Interview Questions Part 19

Q:Exactly what does static variable indicate?
A:There are actually three major purposes of the static.
1. If you declare inside a function:
It keeps the value between function calls
2.If it is declared for a function name:
By default function is actually extern.therefore it is going to be visible from all other files if the function declaration is as static..it is invisible for the external files
3. Static for global variables:
By default we are able to make use of the global variables from outside files when it is static global..that variable is restricted to with in the file.

Q:What are the various storage classes within C?

A:C has 3 kinds of storage: automatic, static and allocated.
Variable getting block scope and also without having static specifier have automatic storage duration.
Variables along with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the static specifier also provide static scope.
Memory extracted from calls to malloc(), alloc() or realloc() is allocated storage class.
 

Q:Can static variables be declared within a header file?
A:you can not declare a static variable without defining it as well (this is really because the actual storage class modifiers static and also extern are mutually exclusive). A static variable could be defined in a header file, but this would cause each source file that included the header file to get its very own private copy from the variable, which is most likely not what was meant

Q:Can a variable become both constant and volatile?
A:Yes. The const modifier implies that this particular program code cannot change the value of the actual variable, but that will not imply that the value can not be changed by means outside this code. For instance, within the example in frequently asked questions, the timer structure was accessed through a volatile const pointer.The function by itself did not change the value of the timer, so it had been declared const. However, the value had been changed by hardware on the computer, so it was declared volatile. If a variable is both equally const and volatile, the two modifiers can come in either order.