We use header and source files to keep code neat and easy to manage. Headers declare what functions and variables are available, and source files contain the actual code.
Header and source file organization
# Header file (example.h) #ifndef EXAMPLE_H #define EXAMPLE_H // Function declarations void say_hello(); #endif // Source file (example.c) #include "example.h" #include <stdio.h> void say_hello() { printf("Hello!\n"); } // Main file (main.c) #include "example.h" int main() { say_hello(); return 0; }
Header files usually have .h extension and contain declarations only.
Source files have .c extension and contain the actual code.
greet so other files can use it.// example.h #ifndef EXAMPLE_H #define EXAMPLE_H void greet(); #endif
greet function.// example.c #include "example.h" #include <stdio.h> void greet() { printf("Hi there!\n"); }
greet function.// main.c #include "example.h" int main() { greet(); return 0; }
This program uses a header file to declare two functions. The source file defines them, and the main file calls them. This keeps code organized and easy to manage.
// greetings.h #ifndef GREETINGS_H #define GREETINGS_H void say_hello(); void say_goodbye(); #endif // greetings.c #include "greetings.h" #include <stdio.h> void say_hello() { printf("Hello!\n"); } void say_goodbye() { printf("Goodbye!\n"); } // main.c #include "greetings.h" #include <stdio.h> int main() { say_hello(); say_goodbye(); return 0; }
Always use include guards (#ifndef, #define, #endif) in header files to prevent errors from including the same file multiple times.
Include only what you need in header files to keep them clean and fast to compile.
Keep function definitions in source files, not in headers, to avoid duplicate code errors.
Header files declare functions and variables for sharing between files.
Source files contain the actual code for those functions.
Organizing code this way makes programs easier to read, reuse, and maintain.
Practice
Solution
Step 1: Understand header file role
Header files declare functions and variables so multiple source files can use them.Step 2: Differentiate from source files
Source files contain the actual code, not declarations.Final Answer:
To declare functions and variables for sharing between source files -> Option BQuick Check:
Header files declare, source files define [OK]
- Confusing header files as containing code
- Thinking header files execute code
- Mixing compiled code with header files
math_utils.h in a C source file?Solution
Step 1: Identify correct include syntax for user headers
User-defined headers use double quotes: #include "filename".Step 2: Differentiate from system headers
System headers use angle brackets, but user headers like math_utils.h use quotes.Final Answer:
#include "math_utils.h" -> Option CQuick Check:
User headers use quotes [OK]
- Using angle brackets for user headers
- Using import instead of #include
- Omitting # in include directive
// math_utils.h
int add(int a, int b);
// math_utils.c
#include "math_utils.h"
int add(int a, int b) { return a + b; }
// main.c
#include
#include "math_utils.h"
int main() {
printf("%d\n", add(3, 4));
return 0;
}What will be the output when running the compiled program?
Solution
Step 1: Understand function declaration and definition
math_utils.h declares add(), math_utils.c defines it to return sum of a and b.Step 2: Trace main function call
main.c calls add(3, 4), which returns 7, then prints it.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Thinking add concatenates numbers
- Expecting compilation error due to missing code
- Confusing printf format
// utils.h
void print_message();
// utils.c
#include "utils.h"
void print_message() {
printf("Hello!\n");
}
// main.c
#include "utils.h"
int main() {
print_message();
return 0;
}When compiling, an error says
implicit declaration of function 'printf'. What is the fix?Solution
Step 1: Identify cause of error
printf is declared in stdio.h; utils.c uses printf but lacks this include.Step 2: Fix by including stdio.h in utils.c
Adding #include <stdio.h> in utils.c provides printf declaration, fixing error.Final Answer:
Add #include <stdio.h> in utils.c -> Option AQuick Check:
Include stdio.h where printf is used [OK]
- Including stdio.h only in header file
- Including stdio.h only in main.c
- Ignoring missing printf declaration
Solution
Step 1: Understand modular organization
Separating declarations in headers and definitions in source files allows sharing and reuse.Step 2: Avoid putting definitions in headers
Definitions in headers cause multiple definition errors when included in many files.Final Answer:
Put all function declarations in one header file and definitions in one source file, then include the header where needed -> Option DQuick Check:
Headers declare, sources define, include headers [OK]
- Defining functions in headers causing linker errors
- Not using headers causing duplicate declarations
- Putting all code in one file reducing modularity
Ready to prove it?
Pass the test and this pattern moves unknown
