Static functions and variables in C - c

I know what is the purpose of using static variables in an object oriented language, still, I don't understand what is the meaning of using the "static" keyword in C.
Can someone explain it to me?

On a function or global variable, static makes the function or global variable local to that file; other files cannot access that function or global variable by that name (but they can access it if you give a pointer to it away).
On a local variable, it makes it act as if it was a global variable, but is only accessible within that function (unless, again, you give a pointer to it away).

The value that a static variable has upon leaving a function is the same value that variable will have the next time the function is called.
A static function can be called only from within the same file that the function appears.

Related

Functions that call each other, declared inside another function

I have two functions that call each other, inside of another function:
int main(){
void aaa();
void b(){
aaa();
}
void aaa(){
b();
}
aaa();
}
(Yes, this example would be stuck in an infinite loop or cause a stack overflow)
The compiler throws the error: static declaration of 'aaa' follows non-static declaration
If I move the function declarations outside of the other function, it works (but I can't do that because those functions need to have access to main's local variables)
Is it possible to make this work?
C does not support nested functions, nor closures.
One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.
You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.
Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.

access Pointers in C programming

I'm new to programming, and am trying to get a better understanding of pointers specifically. Could a static variable be declared inside a function and then accessed or deferenced from outside the function? Why would you not allow explicit pointers to it out of scope, since the memory of the static variable would remain allocated.
Could a static variable be declared inside a function and then accessed or deferenced from outside the function?
The language allows it. So, yes, you can access a pointer to it from outside the function and dereference the pointer from outside the function.
Why would you not allow explicit pointers to it out of scope, since the memory of the static variable would remain allocated.
There are risks associated with that. A calling function can change the state of the static variable. That may or may not be OK depending on the overall structure of your program.
Could a static variable be declared inside a function and then accessed or deferenced from outside the function?
Yes, you can do that.
Explain the scenario where you need this, but in general I don't think its a good approach. Just declare it global.
That question was part of a Homework in Programming 101 when I was in college :)

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.

Static variables clarification

There might be another question like this on stack but I am not completely sure. So on to my question. My professor told everyone, "NEVER USE GLOBAL VARIABLES". But she said that static variables are allowed as long as you give a good enough reason. So my question is, under her criteria, is a static variable declared at the global level ok?
Unfortunately static has two meanings in C. When applied to a global variable, it means that the visibility of this symbol is in the file scope. When applied to a local variable, it means that this variable retains its value between calls (i.e., it's not really a local variable). Your professor is referring to the latter, not the former, when she says they are allowed.
There are times when global variables are useful. Consider stderr: it would be a pain to have to define it in every file you needed to use it in; it is sensibly defined as a global variable.
There are times when it is sensible to store a variable at file scope without external linkage (which you do using static as part of the definition of the variable, outside the scope of any function). For example, if you have a suite of functions which need to share some state but the API does not pass a handle back to the calling code (so there isn't an analogue of open and close — or create and destroy), then one or more static variables at file scope make sense.

monitoring static variable inside a function in eclipse CDT

In c, when a variable is defined static within a function, the value of the variable is retained all the time. So probably, it's stored in the bss or data section.
However, in Eclipse CDT debugging, when trying to inspect this variable, typing the variable name in the expression pane only display the variable when the stack frame is in the function, like local variable.
Is it a bug in eclipse ?
Edit:
In IAR Embedded workbench, static variables in functions can be watched outside the function in debugging, so it's possible.
While the variable is, in fact, stored in the heap... it remains that the scope of the variable is local to the function. It's value has no meaning outside of that function and it should never change when outside of the function... unless you're passing pointers to it around, in which case you can view it in any function that has access to that pointer.
So... no... it's not a bug.
You need to qualify the static variable with the function in order to determine the scope, e.g. foo::myvar if myvar is defined in function foo.

Resources