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

2

66

My C programming book says that when I want to create a static function, I need to put the static keyword in front of the function definition. It doesn't mention anything explicitly about the prototype. Also, the examples don't use prototypes and simply put the static functions at the top of the file (so that they don't need prototypes I am assuming).

So, does a static function need the static keyword for the prototype? Or do I only put it in front of the definition?

Jacksmelt answered 27/3, 2013 at 21:37 Comment(8)
Have you tried compiling void foo(); static void foo() { }?Potted
Not in front of a computer I can do compiling on now.. Also, I've noticed that in programming, just because it works in one example, doesn't mean it will work in all cases.Jacksmelt
Why do it yourself, when you can ask SO to do it for you?Tricky
@w1res while this is true for some cases, this is not one of them. I'd be impressed if you could find a compiler that allows you to omit the static modifier off the declarationDetwiler
An interesting question. I wonder why it got the downvotes.Quinquefid
@Quinquefid which example? void foo(void); static void foo(void) { }? no wayDetwiler
oh that doesn't suprise me. It's not a forward declaration.Detwiler
@75inchpianist, clang lets me do it. I just modified my existing codebase to: void stall(int); ... static void stall(int count) {...}; It gives a warning, but compiles and works. With compilers, we can almost guarantee any particular thing won't work the same in all cases, which is why questions like this exist; to attempt to get at the actual info from behind the scenes, instead of "oh, it worked here, so I'll just assume..." otoh, SO is full of demonstrably false information, including in "accepted" answers, so I agree that in-depth personal testing is superior in almost all cases.Sober
Q
70

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).

Quinquefid answered 27/3, 2013 at 21:55 Comment(4)
static void foo(void) is different from void foo(void), no? two distinct declarations?Detwiler
No, it's just a redundant declaration of the same function.Quinquefid
+1: and a reminder: a function definition serves as a prototype; a prototype serves as a declaration.Edgeworth
Key takeaway: "Normally you will and should have the static in the prototypes too (because they usually come first)."Deanedeaner
D
0

yes, yes you do need to put static in front of the declaration.

type this into ideone.com

int add();
int main(){
    printf("%d",add());
    return 0;
}

static int add(){
    return 1+1;
}

you get this result: http://ideone.com/VzZCiE

now type this

static int add();
int main(){
    printf("%d",add());
    return 0;
}

static int add(){
    return 1+1;
}

you get this: http://ideone.com/sz8HVR

boooom

Detwiler answered 27/3, 2013 at 21:38 Comment(2)
how is it incorrect? If you omit static, it will fail. Even if you think it will work with some compilers, it won't with all compilers. Do you really want to encourage compiler specific code when it can be avoided?Detwiler
You saw the examples in my answer? They contain prototypes without static, yet they are correct and get compiled by every C compiler. I don't want to encourage that, though.Quinquefid

© 2022 - 2024 — McMap. All rights reserved.