Home:ALL Converter>C - Scope of C Functions

C - Scope of C Functions

Ask Time:2016-02-17T01:35:15         Author:Victor3y

Json Formatter

I apologize if this is a beginner's question, but after working in C for a bit, I finally would like a bit of clarification on exactly what kind of files/functions are available to a function.

I understand we can explicitly include other files with the #include macro like so:

#include bar.c

But what about files that are in the same directory? Let's say we have the following file structure:

src-
   |
   a.c
   b.c

And let's say a.c has the function "foo()" and b.c has "bar()"

Would I be able to call bar() within a.c even without explicitly including b.c in the header file?

And what about in sub-directories such as the following:

src-
   |
   a.c
   |
   someFolder-
             |
             b.c

Would I still be able to access bar() in a.c?

Basically, how exactly is the scope of functions (without including them in headers) defined?

Author:Victor3y,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35439304/c-scope-of-c-functions
fuz :

You are confusing scope with linkage.\n\nThe term scope descibes if an identifier is visible to the compiler in a given context. The broadest scope known to the C language is file scope which means that the identifier is visible from the point it is declared to the end of the translation unit after preprocessing. You can make any identifier visible/known by declaring it, but making an identifier visible does not mean that refering to the identifier refers to the thing it refers to in another file.\n\nThe term linkage describes how two identifiers refer to the same thing. There are three levels of linkage: with no linkage, each identifier with the name refers to a different thing. With internal linkage, each identifier within the same translation unit refers to the same thing. With external linkage, each identifier in the whol program refers to the same thing. For two identifiers in two translation units to refer to the same thing, you need to declare both of them to have external linkage.\n\nThis is independent of how the declaration is obtained. There is no difference in writing int foo() in a common header file to writing the same line in both source files. In either case, all identifiers refer to the same foo() as functions have external linkage unless explicitly declared to have internal linkage with the static type specifier.\n\nIt is also independent of how your source code is laid out. As long as all translation units are linked into the same binary, you can call all functions with external linkage in each translation unit.",
2016-02-16T17:44:01
Vasfed :

Depends on whether you are linking these into one binary or not.\n\nIf linking - then yes, functions can be called with implicit declaration.\n\nCommon pattern in this case is making a header(s) file with all declarations, including *.c files is usually not preferred, since #include is just textual inclusion of file contents",
2016-02-16T17:42:03
Petr Skocik :

#include (a preprocessor directive, not a macro, BTW) has a configurable list of directories it searches.\nIf you use the #include \"something.h\" form (as compared to the angle bracket form for including system headers), the first directory in the list is the directory of the including file. \n\nAlthough #include does a simple textual inclusion so it is technically possible to include any text file, c files are almost never included.\n\nIn C, you usually compile c files separately into object files and link them together, and what you do include is header files which consist of declarations, which represent interfaces of the other object files which your current compilation unit otherwise wouldn't see.\n\nCheck out http://www.cs.bu.edu/teaching/c/separate-compilation/ or another article on that topic.",
2016-02-16T17:42:30
yy