Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a static function need the static keyword for the prototype in C?

People also ask

Does keyword is used with static function?

In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class.

Does static function need declaration?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

When and why would you use the keyword static in C?

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory.

What is the need of static function?

A static function is a member function of a class that can be called even when an object of the class is not initialized. A static function cannot access any variable of its class except for static variables. The 'this' pointer points to the object that invokes the function.


No. A function declaration (prototype or even the definition) can omit the keyword static if it comes after another declaration of the same function with static.

If there is one static declaration of a function, its first declaration has to be static.

It is defined in ISO/IEC 9899:1999, 6.7.1:

If the declaration of a file scope identifier for [...] a function contains the storage-class specifier static, the identifier has internal linkage.

[...]

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration.

[...]

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern.

[...]

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

So, e.g. this is valid:

static void foo(void);
void foo(void);
static void foo(void) { }

This one too:

static void foo(void) { }
void foo(void);

static void bar(void);
void bar(void) {}

But this code is incorrect:

void foo(void);
static void foo(void) { }

Normally you will and should have the static in the prototypes too (because they usually come first).