What is a lifetime of object?
c
Solution
There are two kinds of `static` in play here: `static` in the global scope in a file, and `static` inside a function.
The former declares internal linkage for the object, which means that it is only accessible inside the file. These objects are created on bss before `main()` is entered. That memory area is always `memset` to all zeros before `main()` runs.
The default for objects created outside function scope is being global (external linkage), meaning they can be accessed from other compilation units using the `extern` keyword.
`static` inside a function means that the object exists from the first time the function is called until the program ends.
Illustration:
int external_linkage;
static int internal_linkage;
void foo()
{
static int static_in_function;
}
All three variables are guaranteed to have a value of `0` when the program runs, unlike stack and heap variables.
Problem
In this example, what will be difference if variable `string_a` is declared as `static` variable ? ``` const char *pString; void first(void) { const char string_a[] = " First string "; pString =(char *)string_a; } void second(void) { const char string_b[] = " Second string "; pString =(char *)string_b; } int main() { first(); second(); printf("%s\n", pString); } ``` What determined a lifetime of object in C ? What is difference between global and file scope of variables ?