Splitting code into multiple files helps organize the program better, makes it easier to manage, and allows multiple people to work on different parts at the same time.
Click to reveal answer
beginner
What is a header file (.h) used for in C?
A header file contains declarations like function prototypes and variable declarations that other files can use to know how to interact with the code without seeing the full details.
Click to reveal answer
beginner
How do you include one file in another in C?
You use the #include directive with the file name in quotes for your own files, like #include "myfile.h".
Click to reveal answer
beginner
What command compiles multiple C files together?
You can compile multiple files by listing them all in the compiler command, for example: gcc main.c utils.c -o program
Click to reveal answer
beginner
What is the role of a .c file compared to a .h file?
A .c file contains the actual code (function definitions), while a .h file contains declarations (function prototypes and constants) to share with other files.
Click to reveal answer
Which file type usually contains function declarations in C?
A.txt file
B.c file
C.exe file
D.h file
✗ Incorrect
Header files (.h) contain declarations like function prototypes.
How do you tell the compiler to compile multiple C files together?
AList all .c files in the gcc command
BUse #include for .c files
CPut all code in one file
DUse a .h file only
✗ Incorrect
You list all source files in the compiler command, like gcc main.c utils.c -o program.
What does #include "file.h" do in a C program?
ACopies the content of file.h into the current file
BRuns file.h as a program
CCompiles file.h separately
DDeletes file.h
✗ Incorrect
#include literally inserts the content of the header file into the current file before compiling.
Why should you use header files in multi-file C programs?
ATo store executable code
BTo share declarations and avoid repeating code
CTo make the program run faster
DTo hide code from the compiler
✗ Incorrect
Header files share declarations so multiple files know about functions and variables.
What happens if you forget to include a header file with function declarations?
AThe program ignores the missing functions
BThe program runs faster
CThe compiler may give errors or warnings about missing functions
DThe program compiles without any issues
✗ Incorrect
Without declarations, the compiler doesn't know about the functions and will report errors.
Explain how to split a simple C program into two files: one for main and one for helper functions.
Think about separating code and sharing declarations.
You got /5 concepts.
Describe the difference between a .c file and a .h file and why both are important.
Focus on code vs declarations and sharing.
You got /5 concepts.
Practice
(1/5)
1. What is the main reason to split C code into multiple files?
easy
A. To reduce the number of variables
B. To make the program run faster
C. To organize code and make it reusable
D. To avoid using header files
Solution
Step 1: Understand code organization
Splitting code into multiple files helps keep related code parts separate and easier to manage.
Step 2: Recognize code reuse benefit
It also allows reusing code in different programs without rewriting it.
Final Answer:
To organize code and make it reusable -> Option C
Quick Check:
Splitting code = Organize and reuse [OK]
Hint: Think about code clarity and reuse benefits [OK]
Common Mistakes:
Assuming splitting files makes code run faster
Thinking header files are not needed
Believing it reduces variables automatically
2. Which of the following is the correct way to include a header file named utils.h in a C source file?
easy
A. #include utils.h
B. #include
C. include "utils.h"
D. #include "utils.h"
Solution
Step 1: Understand header file inclusion syntax
To include your own header files, use double quotes around the filename.
Step 2: Differentiate system and user headers
Angle brackets are for system headers, double quotes for user headers like utils.h.
Final Answer:
#include "utils.h" -> Option D
Quick Check:
User header files use "filename" [OK]
Hint: Use quotes for your headers, angle brackets for system [OK]
Compiling with gcc main.c gives an error: undefined reference to 'print_message'. What is the likely cause?
medium
A. Missing semicolon in helper.h
B. helper.c was not compiled or linked properly
C. print_message() is not declared in main.c
D. main.c does not include stdio.h
Solution
Step 1: Understand the error message
"undefined reference" means the linker can't find the function definition.
Step 2: Check compilation command
If helper.c is not compiled or linked, the function print_message() is missing at link time.
Final Answer:
helper.c was not compiled or linked properly -> Option B
Quick Check:
Linker error = missing compiled file [OK]
Hint: Always compile and link all source files together [OK]
Common Mistakes:
Not compiling helper.c with main.c
Ignoring linker errors thinking it's a syntax error
Assuming declaration alone is enough
5. You want to split a program into main.c and math_ops.c. math_ops.c defines int add(int a, int b). How should you organize files and compile to use add in main.c correctly?
hard
A. Declare add in math_ops.h, include it in main.c, compile with gcc main.c math_ops.c
B. Define add in main.c and call it from math_ops.c, compile separately
C. Include math_ops.c directly in main.c with #include, compile main.c only
D. Declare add in main.c only, compile math_ops.c separately
Solution
Step 1: Create a header file math_ops.h
Declare the function prototype int add(int a, int b); in math_ops.h.
Step 2: Include math_ops.h in main.c and compile both files
Include math_ops.h in main.c to use add(), then compile with gcc main.c math_ops.c to link both.
Final Answer:
Declare add in math_ops.h, include it in main.c, compile with gcc main.c math_ops.c -> Option A
Quick Check:
Header declares, source defines, compile both [OK]
Hint: Use header for declarations, compile all sources together [OK]
Common Mistakes:
Including .c files instead of headers
Not declaring functions before use
Compiling only one source file
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.