I was told that a function defined as static in one .c file is not accessible from other files. But in the following program, I can access the static void show() function from another file. Is my understanding of static functions in C wrong?
a.h (first file):
static void show()
{
printf("I am in static show function in a.c");
}
b.c (another file):
#include"a.h"
void main()
{
show();
}
Remember that
#includes work by copy-and-pasting the content of the included file. So in your example, after the#includehas been processed, you get this:So clearly
maincan seeshow.1The solution is to not
#include.c files. In general, you should only#includeheader (.h) files. Your static functions shouldn’t be declared or defined in the header file, somainwill not be able to see it.1. However, you now actually have two definitions of the
showfunction, one ina.cand one inb.c. Forstaticfunctions, this isn’t a problem, but for non-staticfunctions you would get a linker error.