Hello!

I am new to C programming and am having a bit of trouble understanding scope and how it relates to header files.

I have a single C file "foo.c" that has probably on the order of 30 different functions that do many different things, but only certain functions are needed within other C files that I am integrating my code with. I also have "definitions.h" and "structs.h" that declare the vast majority of the functions, and those are included in "foo.c". Obviously I don't want to have to include every single function in every single subsequent C file so I can save some resources and make the implementation cleaner overall. Therefore I have written an "Interface.h" file to be included elsewhere (also #included in "foo.c") in the code as an easy way to limit the alterations of other peoples code, but to still give access to my own additions. For example, a function declared in Interface.h could be "RunBoot()" that is defined in foo.c; this function will then run 5 other functions located in foo.c that are invisible to the program that originally called "RunBoot()". Pretty simple right?

Well I then proceeded to whip up a quick test script example.c that has my main() function, and #include's Interface.h. I create an infinite loop that at certain increments will call different functions from interface.h, thereby using other, would-be invisible functions in foo.c; this works just fine. In the advent of seeing what the compiler was actually doing, I threw in a function that was NOT declared in Interface.h and waited for an error of "what the heck is this function, example.c can't see that!" Lo and behold that error did not happen, and it ran the function not defined in Interface.h without a problem??? Then I decided, ok, what happens if I remove the #include Interface.h from example.c? I did that, and every single method I called in example.c ran without a problem with NONE of my header files included in it. Why does example.c seem to have access to everything I've written? I don't want it to, I want it to only be able to see what I manually #include there (interface.h); essentially the only way foo.c and example.c should be allowed to communicate is through functions defined in interface.h, everything else being invisible.

Just what the heck is going on here?

Let me put this into code...


Code:
//foo.c
#include definitions.h
#include structs.h
#include interface.h

void RunBoot() {...} //declared in interface.h
void BootSelected() {...} //declared in definitions.h

// ...
Now onto example.c ...

Code:
//example.c
//include none of the previous header files

int main()
{
     while(1)
     {
          RunBoot(); //Should fail due to lack of #include interface.h, but doesn't?

          //... delay

         BootSelected(); //Should fail due to lack of #include definitions.h, but doesn't either?

     }
}