tags:

views:

262

answers:

6

I was reading some source code files in C and C++ (mainly C)... I know the meaning of 'static' keyword is that static functions are functions that are only visible to other functions in the same file. In another context I read up it's nice to use static functions in cases where we don't want them to be used outside from the file they are written...

I was reading one source code file as I mentioned before, and I saw that ALL the functions (except the main) were static...Because there are not other additional files linked with the main source code .c file (not even headers), logically why should I put static before all functions? From WHAT should they be protected when there's only 1 source file?!

EDIT: IMHO I think those keywords are put just to make the code look bigger and heavier..

+2  A: 

It may be that the author is taking precautions. For example, if someone else is using this file as a source by including it into his main file.

anthares
@Pavel - ah, yes; daring to answer questions - a clear sign of such.
Marc Gravell
@Pavel - I think that not answering a question, just because you may not take some points is "wh*ring". I'm here to learn something, not picking points and ratings!
anthares
Well, I could answer this but I won't. Check my rep graph and think why the second derivative is less than zero. gl.
Pavel Shved
I don't care ... If you don't want to answer - just don't do it. There is no reason to be rude, slapping around with minuses.
anthares
+3  A: 

But there are other files linked with your main module.

In fact, there are hundreds or even thousands, in the libraries. Most of these won't be selected for a small program but all the symbols are scanned by the linker. A collision between a symbol in main and an exported symbol from a library won't by itself cause any harm, but think of the trouble accidently naming something strcpy() could cause.

Also, it probably doesn't hurt to get used to the best-practice styles.

DigitalRoss
+5  A: 

It is hard to guess in isolation, but my assumption would be that it was written by someone who assumes that more files might be added at some point (or this file included in another project), so gives the least necessary access for the code to function. Essentially limiting the public API to the minimum.

Marc Gravell
A: 

Because there are not other additional files linked with the main source code .c file (not even headers), logically why should i put static before all functions ? From WHAT should they be protected when there's only 1 source file?!

You honestly don't need the static keyword in this case.

EDIT: IMHO i think those keywords are put just to make the code looks bigger and heavier..

However, if you really want to read more about static keyword you can start with a book. Some more info on the keyword static at #2216239 -- may help!

dirkgently
Ok, now my time to crib about the double downvotes. Can someone explain what is wrong with this post? I refer to another SO answer of mine. Not a blog post.
dirkgently
I guess that people are objecting to the "You honestly don't need the static keyword in this case" part of the answer. At that point, it becomes close to zealotry; I use static myself, even in single source file programs, but I don't require everyone else to do so. I suppose people might be objecting to the 'shameless plug' part of the cross-reference. Maybe if it was reworded, that would alter things. I have a suspicion that you're suffering from 'the thundering herd' effect; once one down-vote with no commentary occurs, others can easily follow. Irksome!
Jonathan Leffler
I've not downvoted you. But "don't need" != "should get rid of".
Johannes Schaub - litb
@Jonathan and litb: Edited post. Also, in case this is not clear, no I am not trying to suggest either way, except for the fact that in a single file toy program, you can do away with `static`s.
dirkgently
+13  A: 

If a function is extern (default), the compiler must ensure that it is always callable through its externally visible symbol.

If a function is static, then that gives the compiler more flexibility. For example, the optimizer may decide to inline a function; with static, the compiler does not need to generate an additional out-of-line copy. Also, the symbol table will smaller, possibly speeding up the linking process too.

Also, it's just a good habit to get into.

ephemient
+2  A: 

As a coding rule that I follow, any function (other than main()) that is visible outside its source file needs a declaration, and that declaration should be in a header. I avoid writing 'extern' declarations for functions in my source files it at all possible, and it almost always is possible.

If a function is only used inside a single source file, it should be static. That makes it much easier to modify; you know that the only place you need to look to see how it is used is the source file you have in front of you now (unless you make a habit of including '.c' files in other '.c' files - which is also a bad habit that should be broken now).

I use GCC to help me enforce the coding rule:

gcc -m64 -Wall -Wextra -std=c99 -Wmissing-prototypes -Wstrict-prototypes

That's a fairly typical collection of flags; I sometimes use -std=c89 instead of -std=c99; I sometimes use -m32 instead of -m64; I don't always use -Wextra (but my code is moving in that direction). I always use -Wmissing-prototypes and -Wstrict-prototypes to ensure that each external function is declared before it is defined or used (and each static function is either declared or defined before it is used). I occasionally use -Werror (so if the compile emits a warning, the compilation fails). I could use it more than I do since my code does compile without warnings - or gets fixed so that it does.

So, you could easily have been looking at my code. In my code, the only functions that are exposed - even in single source file programs - are the functions that are declared in a header, which means that they are part of the external interface to the module that the source file represents.

Jonathan Leffler