João Pires
João Pires

Reputation: 1007

Is the static keyword needed in both the declaration and the definition of a static function in C?

In C, inside a implementation file, when forward declaring a static function in that same file, is the static keyword needed in both the declaration (prototype) and the definition of the function?

Upvotes: 6

Views: 836

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51825

If you include static in the prototype (forward declaration), then you can omit that keyword in the actual definition (it is then implied). However, if you do not have static in the prototype but do include it in the definition, then you are in the realms of non-Standard C.

For example, the following code is non-standard:

#include <stdio.h>

void foo(void); // This declares a non-static function.

int main()
{
    foo();
    return 0;
}

static void foo(void)
{
    printf("Foo!\n");
}

The clang-cl compiler warns about this:

warning : redeclaring non-static 'foo' as static is a Microsoft extension [-Wmicrosoft-redeclare-static]

Upvotes: 2

Related Questions