What is the purpose of linking multiple files in C programming?
Linking multiple files allows you to split your program into separate source files, making it easier to organize, maintain, and reuse code. The linker combines these files into a single executable.
Click to reveal answer
beginner
How do you compile and link two C files named main.c and helper.c using GCC?
Use the command: gcc main.c helper.c -o program. This compiles both files and links them into one executable named program.
Click to reveal answer
beginner
What is the role of header files (.h) when linking multiple C files?
Header files declare functions and variables so that different source files know about each other's code. They help the compiler check for correct usage before linking.
Click to reveal answer
intermediate
What happens if you forget to link a source file that contains a function used in another file?
The linker will give an 'undefined reference' error because it cannot find the function's code to include in the final program.
Click to reveal answer
intermediate
Explain the difference between compiling and linking in the context of multiple C files.
Compiling translates each source file (.c) into an object file (.o) separately. Linking combines all object files into one executable, resolving references between them.
Click to reveal answer
Which command correctly compiles and links two C files named a.c and b.c into an executable named app?
Agcc a.c b.c -o app
Bgcc a.c -o app
Cgcc b.c -o app
Dgcc a.o b.o
✗ Incorrect
Option A compiles both source files and links them into the executable 'app'.
What file type is produced after compiling a C source file but before linking?
AExecutable file (.exe)
BObject file (.o)
CHeader file (.h)
DSource file (.c)
✗ Incorrect
Compiling produces object files (.o) which are then linked to create the executable.
Why do we use header files when working with multiple C files?
ATo declare functions and variables for sharing between files
BTo link files automatically
CTo compile faster
DTo store executable code
✗ Incorrect
Header files declare functions and variables so different source files can use them correctly.
What error occurs if a function is used but its source file is not linked?
ASyntax error
BRuntime error
CUndefined reference linker error
DSegmentation fault
✗ Incorrect
The linker cannot find the function definition, causing an undefined reference error.
Which step combines multiple object files into one executable?
APreprocessing
BDebugging
CCompiling
DLinking
✗ Incorrect
Linking combines object files and resolves references to create the final executable.
Describe the process of compiling and linking multiple C files to create a single program.
Think about how separate pieces come together to form a complete program.
You got /4 concepts.
Explain why header files are important when working with multiple C source files.
Consider how files communicate their available functions to each other.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of linking multiple files in a C program?
easy
A. To organize code into separate files and combine them into one program
B. To run multiple programs at the same time
C. To convert C code into machine language
D. To write comments in different files
Solution
Step 1: Understand code organization
Linking multiple files allows splitting code into parts for better organization and reuse.
Step 2: Understand linking process
After compiling separately, linking combines these parts into one executable program.
Final Answer:
To organize code into separate files and combine them into one program -> Option A
Quick Check:
Linking = Combining files [OK]
Hint: Linking combines separate code files into one program [OK]
Common Mistakes:
Confusing linking with running multiple programs
Thinking linking converts code to machine language
Believing linking is for writing comments
2. Which command correctly compiles two C files main.c and utils.c together using GCC?
easy
A. gcc main.c -o program
B. gcc -o program -c main.c utils.c
C. gcc main.c utils.c -o program
D. gcc program -o main.c utils.c
Solution
Step 1: Understand GCC command structure
The correct syntax is: gcc [source files] -o [output file].
Step 2: Check options order
The standard GCC syntax is gcc [source files] -o [output], as in gcc main.c utils.c -o program.
When compiling separately and linking, it gives an error. Why?
medium
A. Cannot compile multiple files separately
B. Missing #include in file1.c
C. main() should be in file1.c
D. greet() is not declared before use in file2.c
Solution
Step 1: Check function declaration in file2.c
file2.c calls greet() but does not declare it or include a header with its prototype.
Step 2: Understand compiler error
Without declaration, compiler errors occur because it doesn't know greet() signature.
Final Answer:
greet() is not declared before use in file2.c -> Option D
Quick Check:
Declare functions before use [OK]
Hint: Always declare functions before calling them [OK]
Common Mistakes:
Assuming definition is enough without declaration
Ignoring missing #include (not cause here)
Thinking main() must be in same file as greet()
5. You have three files:
// math_ops.c
int add(int a, int b) { return a + b; }
// math_ops.h
int add(int a, int b);
// main.c
#include
#include "math_ops.h"
int main() {
printf("Sum: %d\n", add(3, 4));
return 0;
}
Which is the correct way to compile and link these files to produce an executable named calc?