i am looking at static functions, i know they have a scope limited to the file they are declared in. For clarification i use code blocks with GCC. If i declare the function in a ..c file and include it in my main.c file, the function can be accessed (but the compiler will complain if a non static function is defined, as i have multiple definitions). However if i have a c file with some static function and another non-static function, then the static function is not accessible and the non-static function is accessible. This to me is a rather weird. I know that the #include directive copies the contents of the to-be-included file in to the file where the include directive is declared. But then why can i access the non-static function without including the .c file in the main.c file? Any suggestions on where i could read up on this topic? I am thinking it has something to do with linking, but i may be wrong.
-
3Posting code that demo's the issue would make the question and potential answer's more clear. – chux - Reinstate Monica Jul 16 '21 at 03:35
-
1you should not `#include` a .c file so this is moot – M.M Jul 16 '21 at 03:38
-
3The code is worth a thousand words. – dbush Jul 16 '21 at 03:38
-
I guess the question is going to turn out to be "why can I call an undeclared function in another unit" – M.M Jul 16 '21 at 03:58
2 Answers
When a function is declared without static
, it has external linkage.1 This means that, when the program is linked2, the same name in different translation units3 can be made to refer to the same thing.
When you define a function in one source file, say int square(int x) { return x*x; }
, compiling that source file produces an object file that contains information saying “The function foo
is defined here.” When you declare a function in another source file and use that function, such as int square(int x); … b = square(a);
, compiling that source file produces an object file that contains information saying “The function foo
is used here.” When the linker or program loader processes these object files, it modifies the place where foo
is used to refer to the place where foo
is defined.
If you use a function without declaring it in that translation unit, your compiler might provide a default declaration for the function, as if it had been declared to return int
with no information about its parameters. This behavior is a legacy of old versions of C. Quite likely your compiler issues a warning such as “implicit declaration of function” for this. You should not ignore this warning. When it appears, find the code causing the warning and fix it—either correct the typing of a misspelled function name or insert a declaration for the function.
When using GCC or Clang, you should use the switch -Werror
to elevate warnings to errors, so that the compiler will not compile programs that cause warning messages. While this may at first make it more of a nuisance to get your programs compiled, it causes you to fix errors early and to learn to program correctly.
Footnotes
1 With static
, an identifier for a function has internal linkage. The rules for identifiers for objects (which you may think of as variables) are more complicated, and they can also have no linkage.
2 In practice, each compilation produces an object module. Linking combines object modules to make an executable program or, in complicated builds, new object modules.
3 A translation unit is the source file being compiled including all the files it includes with #include
statements.

- 195,579
- 13
- 168
- 312
So I found a decent answer to your question in a different stackoverflow question, but before I get to that I want to explain why including .c files is bad practice, and shouldn't be done:
- As you pointed out, #include copies the content of the file. This means that you can't include the .c file more than once, since you would be creating >1 instance of the same function (of the same name).
- If you try to create a universal .c file, say, math.c, you can't create any other .c files that use any of its functions, since you can only include it once, and thats reserved for your main.c file. This severely limits the usefulness of every function in your .c files
- instead, you should only ever include header files (.h). You declare your functions in your .h file, define in your .c file.
- Whatever functions you declare in your .h file, you can then use in any other files where said .h file is included, without causing compiler errors
- I get that this is confusing, but here's examples from another stackoverflow thread
- here's a further better document explaining how header files (.h) work
- Why can you access non-static function despite not including the .c file?
- this should not be possible generally in c if you're not explicitly including said .c file. If I were to do that I would get an "implicit declaration" function, because the compiler doesn't recognize the functions, as they've not been declared previously.
- long story short, declaring a function static in a .c file means that its scope is limited only to said .c file, you can't call it anywhere else

- 52
- 5