C #include
In this page:
#include <standard_header.h> // system headers
#include "custom_header.h" // your own headers
Including Standard Headers
Angle brackets tell the compiler to look in its predefined system include directories first (like /usr/include), which is where headers for the standard library, like stdio.h, actually live on your system.
Example: Including Standard Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Uses angle brackets for system headers");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Including Custom Headers
Double quotes tell the preprocessor to search relative to the current file's own directory first before falling back to the system directories -- this is the convention for headers that are part of your own project rather than the standard library.
Example: Including Custom Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Custom headers use double quotes");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Header Expansion
This nested expansion can create deep chains -- including one header might pull in several others, which is normal, but it's also how accidental circular includes (A includes B, which includes A) can cause infinite expansion without proper guards.
Example: Nested Header Expansion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("stdio.h itself may include other headers");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Preventing Multiple Includes
Without include guards, defining the same struct or function twice from two separate include chains that both reach the same header causes a redefinition compiler error -- this is one of the most common beginner compilation errors in multi-file C projects.
Example: Preventing Multiple Includes
// Include standard input/output (printf, scanf)
#include <stdio.h>
#ifndef MYHEADER_H
// Define the macro MYHEADER_H (replaced by the preprocessor)
#define MYHEADER_H
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Include guard prevents duplicate definitions");
// Return 0 to signal that the program finished successfully
return 0;
}
#endif
Login to try C/C++/Java/PHP code in the editor
Best Practices for Headers
A well-organized header typically contains only function prototypes, type definitions, and macro constants -- putting actual function code in a header causes 'multiple definition' linker errors if that header is included in more than one .c file.
Example: Best Practices for Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Headers should hold prototypes, not function bodies");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using angle brackets for your own header instead of quotes, so the compiler searches the system folders and does not find it.
- Forgetting include guards, which causes redefinition errors when a header is included twice.
- Putting function definitions in a header that several files include, causing duplicate definitions at link time.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: