A header file in C is a file with extension .h that contains declarations of functions, macros, and variables. It helps share code between different source files.
Click to reveal answer
beginner
What does the #include directive do?
The #include directive tells the compiler to copy the contents of a specified header file into the source file before compilation. It helps reuse code and declarations.
Click to reveal answer
intermediate
Difference between #include <file.h> and #include "file.h"?
#include <file.h> searches for the header file in system directories. #include "file.h" searches first in the current directory, then system directories.
Click to reveal answer
intermediate
Why use header guards in header files?
Header guards prevent a header file from being included multiple times in the same source file, avoiding errors like redefinition of functions or variables.
Click to reveal answer
beginner
Example of a simple header guard in a header file?
#ifndef MYHEADER_H
#define MYHEADER_H
// Declarations here
#endif
This code ensures the header content is included only once.
Click to reveal answer
What is the main purpose of a header file in C?
ATo store function definitions
BTo execute code
CTo declare functions and macros for sharing
DTo compile the program
✗ Incorrect
Header files declare functions and macros so multiple source files can use them.
Which directive includes a header file from the system directories?
A#include <file.h>
B#define file.h
C#import <file.h>
D#include "file.h"
✗ Incorrect
#include <file.h> searches system directories for the header file.
What problem do header guards solve?
AAllow dynamic linking
BSpeed up compilation
CMake code run faster
DPrevent multiple inclusions causing errors
✗ Incorrect
Header guards stop the same header file from being included more than once.
Where does the compiler copy the contents of a header file?
ABefore compilation
BDuring linking
CAt runtime
DAfter compilation
✗ Incorrect
The preprocessor copies header file contents into the source file before compilation.
Which is the correct way to write a header guard?
A#include HEADER_H
B#ifndef HEADER_H
#define HEADER_H
#endif
C#define HEADER_H
#ifndef HEADER_H
D#ifdef HEADER_H
#define HEADER_H
✗ Incorrect
The pattern #ifndef ... #define ... #endif is used for header guards.
Explain what a header file is and why we use the #include directive in C.
Think about how multiple files share code.
You got /4 concepts.
Describe how header guards work and why they are important.
Imagine including the same file twice by mistake.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a header file in C programming?
easy
A. To store declarations and share them between source files
B. To execute the main program logic
C. To compile the program faster
D. To store variable values during runtime
Solution
Step 1: Understand what header files contain
Header files usually contain declarations like function prototypes and macros, not executable code.
Step 2: Understand their role in sharing code
They allow multiple source files to share these declarations, so the compiler knows about functions and variables.
Final Answer:
To store declarations and share them between source files -> Option A
Quick Check:
Header files = declarations shared [OK]
Hint: Header files hold declarations, not code [OK]
Common Mistakes:
Thinking header files contain executable code
Confusing header files with source files
Believing header files speed up compilation
Assuming header files store variable values
2. Which of the following is the correct syntax to include a system header file named stdio.h?
easy
A. #include "stdio.h"
B. #include
C. include
D. #include stdio.h
Solution
Step 1: Recall syntax for system headers
System headers use angle brackets <> to tell the compiler to look in system directories.
Step 2: Check each option
#include uses #include with <stdio.h>, which is correct syntax. #include "stdio.h" uses quotes which are for user headers. Options C and D miss the # or angle brackets.
Final Answer:
#include <stdio.h> -> Option B
Quick Check:
System headers use <> [OK]
Hint: Use < > for system headers, " " for your own [OK]
void printMessage() {
printf("Hello from header!\n");
}
medium
A. No output
B. Compilation error: function not declared
C. Runtime error
D. Hello from header!
Solution
Step 1: Understand #include "myheader.h" effect
The content of myheader.h is inserted, so printMessage() is defined before main.
Step 2: Check function call and output
printMessage() prints "Hello from header!\n", so the program outputs that line.
Final Answer:
Hello from header! -> Option D
Quick Check:
Included function runs and prints message [OK]
Hint: Including header inserts code, so functions are available [OK]
Common Mistakes:
Assuming function is undeclared without prototype
Thinking header files can't contain function definitions
Confusing compile and runtime errors
Ignoring the newline in output
4. Identify the error in this code snippet:
#include
#include
int main() {
printHello();
return 0;
}
Assuming myheader.h is a user-defined header file in the project folder.
medium
A. Function printHello() is not defined
B. Missing semicolon after #include directives
C. Using angle brackets for a user-defined header file
D. Missing main function return type
Solution
Step 1: Check #include syntax for user headers
User headers should be included with quotes "myheader.h" to search local directories first.
Step 2: Understand effect of wrong include syntax
Using angle brackets makes compiler look in system directories, so it may not find myheader.h causing an error.
Final Answer:
Using angle brackets for a user-defined header file -> Option C
Quick Check:
User headers use "" not <> [OK]
Hint: Use "" for your headers, < > for system headers [OK]
Common Mistakes:
Using < > for user headers
Thinking #include needs semicolons
Assuming printHello() is always undefined
Ignoring main return type
5. You want to create a header file mathutils.h that declares a function int add(int a, int b); and use it in two source files main.c and calc.c. Which is the best practice to avoid multiple definition errors?
hard
A. Put only the function declaration in mathutils.h and define the function in one source file
B. Define the function in both source files and include mathutils.h everywhere
C. Put the function definition in mathutils.h and include it in both source files
D. Include mathutils.h only in main.c and define function in calc.c
Solution
Step 1: Understand declarations vs definitions in headers
Headers should contain declarations (function prototypes), not definitions (function bodies), to avoid multiple definitions.
Step 2: Apply to multiple source files
Define the function in one source file (e.g., calc.c) and declare it in mathutils.h included by both files.
Final Answer:
Put only the function declaration in mathutils.h and define the function in one source file -> Option A
Quick Check:
Declarations in headers, definitions in one source file [OK]
Hint: Declare in header, define once in source file [OK]
Common Mistakes:
Defining functions in headers causing multiple definitions
Not including header in all source files needing declaration
Defining function in multiple source files
Including header only in one source file
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.