Concept
How do header and source files organize functions in C?
ComputerScienceOne / Calling Functions
"18.1.3. Organizing Functions The separation of a function declaration (prototype) and a function definition provides a natural way to organize functions in C. We place prototypes into a header file which has a file extension .h and then place the corresponding function definitions into a source file with the file extension, .c . We’ve seen this before with the standard libraries: we use #include <math.h> to “include” the math library’s header file in our code. This essentially brings in the math library function prototypes so that we can write calls to functions like sqrt() or sin() . Only when we compile do we actually need to link our code to the function definitions. When we separate prototypes into header files and definitions into source files we also need to “include” the prototypes in our source file just as we would need to include them in any other file in which we use one of the functions. Suppose our functions above have their prototypes in a file named utils.h and their definitions in a file named utils.c . In the utils.c source file we would typically use the following syntax to include the header file: #include \"utils.h\" We use the double quote syntax with user-defined libraries while the usual less-than/greater- than syntax is used with standard libraries. With the less-than/greater-than syntax, the compiler will attempt to look for the header file(s) in a specified system directory which it will fail to find if it is a user-defined library. Furthermore, other elements are usually included in header files such as preprocessor directives and other declarations (such as enumerated types and structures which we introduce later)."
Related Ideas
- What are preprocessor directives and why are standard libraries included?ComputerScienceOne · Preprocessor Directives
- How are functions defined and declared in modern C?ComputerScienceOne · Declaration: Prototypes
- What information does a C function prototype specify?ComputerScienceOne · Declaration: Prototypes
- How is a C function prototype turned into a function definition?ComputerScienceOne · Declaration: Prototypes
- How do you call functions in C?ComputerScienceOne · Organizing Functions
- How do you use the C standard mathematics library?ComputerScienceOne · Preprocessor Directives
- How are void functions defined in C?ComputerScienceOne · Void Functions
- What capabilities and constraints do functions have in C?ComputerScienceOne · Declaration: Prototypes