Google News
logo
Embedded C - Interview Questions
What are some interesting facts about static variables in C?
Following is a list of some interesting facts about static variables in C :
 
* There are two types of static variables, static int variable and static auto variable. A static int variable remains in memory while the program is running and a normal or auto variable is destroyed when a function call declared is over. For example, we can use a static int variable to count the number of times a function is called, but we cannot use an auto variable for this purpose.

* Static variables are allocated memory in the data segment, not the stack segment.

* The default value for static variables is 0. Like global variables, they are initialized as 0 if not initialized explicitly.

* In C language, we can initialize the static variables using only constant literals.

* Static global variables and functions are also possible in C++. They are mainly used to limit the scope of a variable or function to a file.

* We should not declare static variables inside the structure because the C compiler requires the entire structure elements to be placed together.
Advertisement