C - Scope of C Functions
NickName:Victor3y Ask DateTime:2016-02-17T01:35:15

C - Scope of C Functions

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?

Copyright Notice:Content Author:「Victor3y」,Reproduced 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

Answers
fuz 2016-02-16T17:44:01

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.",


Vasfed 2016-02-16T17:42:03

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",


Petr Skocik 2016-02-16T17:42:30

#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.",


More about “C - Scope of C Functions” related questions

C - Scope of C Functions

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

Show Detail

Scope of variables inside anonymous functions in C#

I have a doubt in scope of varibles inside anonymous functions in C#. Consider the program below: delegate void OtherDel(int x); public static void Main() { OtherDel...

Show Detail

Functions declared within a function in C, are they usable at a global scope?

I've been running into a minor problem with regards to seeing people, currently helping a teacher out through grading, and seeing people declaring functions within the scope of main, instead of wit...

Show Detail

What does scope of functions mean?

What does scope of functions mean? I understand the scope of variables. When we talk about the scope of functions, is it referred to the functions within structures (classes) or is there scope for...

Show Detail

Why nested functions in C are against C standards

Nested functions(function declarations in block scope) are not allowed in C standards(ANSI[C89], C99, C11). But I couldn't find stating it in C standards. Edit : Why a function definition cannot...

Show Detail

variables' scope and place the following code inside one of the functions in scope.js so the output is a: 1, b: 8, c: 6

var a = 1, b = 2, c = 3; (function firstFunction(){ var b = 5, c = 6; (function secondFunction(){ var b = 8; (function thirdFunction(){ var ...

Show Detail

Scope Guard in C

I would like to use scope guard in C in order to do profiling. I would like to know how much time I spend in a function. Here is what I do: int function() { tic(); ... do stuff ... if (

Show Detail

Reasons to use Static functions and variables in C

I wonder about the use of the static keyword as scope limiting for variables in a file, in C. The standard way to build a C program as I see it is to: have a bunch of c files defining functions and

Show Detail

Functions scope at a file level in C#/.NET?

I have many many functions (100+ estimated) and more then half are only needed in the file its defined. Is there a way to define functions so it scope is at a file level?

Show Detail

Should functions be declared with a minimal scope?

In my experience, most JS functions, constructor functions, or module-pattern functions, are declared as globals or are bound to a global namespace object. In any case, these identifiers are globally

Show Detail