0
0
Cprogramming~5 mins

Header files and include directive - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a header file in C?
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.<br>#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
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"
What problem do header guards solve?
AAllow dynamic linking
BSpeed up compilation
CMake code run faster
DPrevent multiple inclusions causing errors
Where does the compiler copy the contents of a header file?
ABefore compilation
BDuring linking
CAt runtime
DAfter 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
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.