The static keyword when used with a function definition says that the function has file level scope. This means the function name is only visible within the file itself. The static keyword used in a function definition modifies the function name visibility and does not modify the function return type.
So a function declared static can return any kind of a type.
The use of static on a variable defined in a function body is used to indicate that the variable is to be created at the point when the application is loaded and started. So if you use the static modifier on a variable within a function, that variable is not created on the stack when the function is called. It exists independently of when the function is called. However the variable's visibility is only within the function.
In your example you have a function that is returning the address of a static variable. You can do this however you must understand that this use is not thread safe. In other words all threads calling the function will get the same variable at the same memory location and not their own version of the variable.
You must also understand that if you return the address of a static variable you can also cause problems with reentrancy and recursiveness. The reason is that variables on the stack provide for reentrancy and recursiveness since each time the function is called, a new frame is added to the stack so each function call has its own stack frame hence its own set of variables.
This is a known issue with the old strtok() function in the Standard C library which used a variable within the strtok() function to maintain a state between calls using NULL as the string address of where the last call to strtok() left off when parsing a string. I have seen an issue where a function called strtok() to begin parsing a string and then called another function which in turn called strtok() to begin parsing a different string. The result was some really strange errors and behavior until the cause was figured out.
Using the static modifier on a global variable within a file will create a global variable that can be shared by multiple functions within the file. However like using the static modifier on a function name, a static variable will only have file scope visibility.
// .. top of a file of C source code
static int aStaticInt = 0; // a static int that can be shared by all functions in the file but is visible only in the file
int aNonStaticInt = 0; // a non static int that is visible outside of the file
static int myStaticFunc (void)
{
// a function that is visible only within the file
}
int myNonStaticFunc (void)
{
// a function that is visible outside the file as well as inside the file
}
static char *isn't a typevoidis redundant and makes some compilers break.fn1(void), in C89 at least, means that I am declaring a function that takes no parameter, whilefn1()means that the parameters are unspecified (that is, in old times when the idea of prototype has not been invented). Am I wrong?fn1(void)from olden days. Whether it is C89 or just malpractice, I don't know. But I have seen recent some compiler barf on it, maybe Visual Studio.