Is a variable inside a static function a static variable? - c

Is a variable inside a static function a static variable?
I have a interrupt handler function, which is a static function. And there are some variables in it. I am not sure whether it is necessary to spin lock these variables in the function to prevent multiple access.

An object defined inside a block (each function definition is a block) does not have static storage duration unless it is declared with static.
If it is declared without a storage-class specifier, it has automatic storage duration.

The static keyword, when applied to a local variable in a function, puts it in the process’ data section rather than in stack memory.
This means it will persist beyond its scope and returning an address to a static array or string is defined behaviour.
It has nothing to do with the static qualifier on the function declaration, which makes it visible only to the translation unit it appears in. Variables in a static function are not automatically static themselves.

Related

Why and where to use a global variable and a static global variable in C?

I have read about global and local variables and static keyword, but i dont know which is the difference between something like this:
int x; //Global variable
int main(){
//Code...
}
And this:
static int x; //Static Global variable
int main(){
//Code...
}
I know some properties of the static keyword, and i know why is useful for local variables, but i dont know which is the use for global variables
I have read that using static for global variables is used for accesing the variable only in the C file it is declared/defined, but, i mean, if we dont use extern we just cant access that global variable in other c files anyway
I think that a "static global" variable is used to prevent the use of the extern keyword in order to prevent the use of that variable in multiple C files, is that correct?
Thanks in advance!
"Global" is not a formal term, but it generally means "variable that can be accessed anywhere". Therefore global.
To dive into some formal terms:
The formal and correct term for a variable declared outside a function is declared at file scope.
What formally defines how a variable may be accessed in C is referred to as linkage.
A variable declared at file scope generally has external linkage and can be referred to from elsewhere with extern.
So the correct term to use for your first example is "file scope variable with external linkage".
Whenever we add the storage class specifier static to a declaration, we force the variable or function to instead get internal linkage. This means that it is only accessible from within the translation unit (the .c file and all .h files it includes) where it was declared.
It is the opposite of global, so saying "static global" doesn't make any sense. You cannot refer to such a variable with extern. Instead the correct term is "file scope variable with internal linkage".
The main purpose of static is indeed private encapsulation, since the internal linkage guarantees that the variable or function cannot be accessed by other .c files.
But static also at the same time gives static storage duration to a variable, meaning it is guaranteed to persist throughout the execution of the program and that it has some initialization rules guaranteed. Now as it happens, variables at file scope always gets static storage duration, static or not, so it isn't very relevant to your example. But as you earlier discovered, static storage duration does make a big difference for local variables, since their value will then be preserved throughout multiple function calls.
More details here: What does the static keyword do in C?
Yes, it is correct.
A global static variable can only be accessed in the file where it is created (file scope).

Static global in memory

If there are 2 files in a project, using same name for static variable in both files, I didn't get linker error, but I want to know how two static variables named in memory.
Static variables are not visible outside the C file where they are declared (or even outside the scope, with "C file" being "a particular file's file scope"). So there is no collision, and of course variable names don't matter in compiled code.
The static variable is visible only within the module, it provides the local linkage.
The static variable inside the function it retains the value during function call.
The Static global variable or the function is visible only in the file or module that is declared.
The static specifier specifies both static storage duration and internal linkage.
Static storage duration. The storage duration is the entire execution of the program, and the value stored in the object is initialized only once, prior to main function.

What the word "static" in static class means?

I know what static classes are I would like to know more about the word "static".
When I search for the word "static" I get many definitions, from most of them I get that static is something that doesn't move (fixed in place). Is it right?
What does the word mean? Where did the word came from historically?
Thank you in advance.
Static is a keyword used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following, Static variable in functions.
In the C programming language (and its close descendants such as C++ and Objective-C), static is a reserved word controlling both lifetime (as a static variable) and visibility (depending on linkage). The word static is also used in languages influenced by C, such as Java.
In C, static is a storage class (not to be confused with classes in object-oriented programming), as are extern, auto and register (which are also reserved words). Every variable and function has one of these storage classes; if a declaration does not specify the storage class, a context-dependent default is used:
extern for all top-level declarations in a source file,
auto for variables declared in function bodies.
Storage class Lifetime Visibility
extern program execution external (whole program)
static program execution internal (translation unit only)
auto, register function execution (none)
In these languages, the term "static variable" has two meanings which are easy to confuse:
A variable with the same lifetime as the program, as described above (language-independent); or
(C-family-specific) A variable declared with storage class static.
Variables with storage class extern, which include variables declared at top level without an explicit storage class, are static in the first meaning but not the second.
The static keyword when prefixed while declaring a variable or a function can have other effects depending on where the declaration occurs.
Static global variable
A variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage").
Static local variables
Variables declared as static inside a function are statically allocated, thus keep their memory cell throughout all program execution, while having the same scope of visibility as automatic local variables (auto and register), meaning remain local to the function. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
Static member variables
Member variables declared as static inside class definitions are class variables (shared between all class instances, as opposed to instance variables).
Static function
Similarly, a static function -- a function declared as static at the top level of a source file (outside any class definitions) -- is only visible throughout that file ("file scope", also known as "internal linkage").
Static method
Similarly, a static method -- a method declared as static inside a class definition -- are meant to be relevant to all instances of a class rather than any specific instance.

Assignment to static variable is ignored

I have following piece of code:
#include <stdio.h>
int f1()
{
static int s=10;
printf("s=%d\n",s++);
}
int main()
{
f1();
f1();
return 0;
}
The output is:
s=10
s=11
Why is the line static int s=10 ignored at the second time, when f1 is called?
That is no assignment, but an initializer. Local static variables are only initialized once at program startup like global variables. They keep their last assigned value even between invocations of the function. Thus after your first call, it retains the value 11. In fact, they are like file-scope static variables, with their name only known in the scope of the block they are declared (but you can pass them by pointer).
Drawback is they only exist once. If you invoke the same function from multiple threads, they all share the same variable.
Try a third call: you will get 12.
Note: the initializer must be a constant expression. Try static int s = 10, t = s + 5; and read the compiler error message.
Initialization of a static variable is one-time (with the time of initialization guaranteed to occur before the first call, which could occur at compile time or at run time; compiler dependent). That's the main reason to use them.
The static variables are initialized only once, conceptually even before application has started.
From C11 (N1570) §5.1.2/p1 Execution environments:
All objects with static storage duration shall be initialized (set to
their initial values) before program startup.
along with §6.2.4/p3 Storage durations of objects:
Its lifetime is the entire execution of the program and its stored
value is initialized only once, prior to program startup.
As others have said, a static variable at function scope is initialized only once. So the assignment doesn't happen on subsequent calls to the function.
Unlike other local variables, a static local is not defined on the stack but in the data segment, probably in the same location as global variables. Globals are also initialized at application startup (they have to, since they don't live inside of a function and therefore can't be executable code), so conceptually you can think of a static variable as a global variable with limited visibility.
From the C89 Standard HTML version at 3.1.2.4 Storage durations of objects it specifies:
An object declared with external or internal linkage, or with the storage-class specifier static has static storage duration. For such an object, storage is reserved and its stored value is initialized only once, prior to program startup. The object exists and retains its last-stored value throughout the execution of the entire program
(The emphasis is mine)
So it says that everytime you use the static qualifier, that variable preserves its value across multiple function calls.
Local variables that are not static are initialized everytime you call the function that delcares them, so they do not preserve their value across function calls.
Hope this helped!

Why function parameters can not be static

Can anyone please tell me that why the functions parameters can not be static?
Is this the reason that function parameters are declared on Stack and gets de-allocated when function return? There is no way to retain parameter values? Just confused. Please clarify.
Thanks.
The keyword static can probably be seen as somewhat "overloaded".
The following usage-options are all viable:
Static local variables
Static global variables
Static member variables
Static global functions
Static member functions
Variables:
In terms of runtime, all types of static variables are essentially the same. They all reside in the data-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
Static local variable: recognized by the compiler only in the scope of the function
Static global variable: recognized by the compiler only in the scope of the file
Static member variable: recognized by the compiler only in the scope of the class
Functions:
In terms of runtime, all types of functions (static and non-static) are essentially the same. They all reside in the code-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
Static global function: recognized by the compiler only in the scope of the file
Static member function: recognized by the compiler only in the scope of the class
As to your question, arguments are passed to a function in the stack. There is no sense in having them static, because that would effectively place them in the data-section. And if they are located in the data-section, then the function can simply read them from there instead of having them passed to it in the stack.

Resources