C #include
In this page:
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 <stdio.h>
int main() {
printf("Uses angle brackets for system headers");
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 <stdio.h>
int main() {
printf("Custom headers use double quotes");
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 <stdio.h>
int main() {
printf("stdio.h itself may include other headers");
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 <stdio.h>
#ifndef MYHEADER_H
#define MYHEADER_H
int main() {
printf("Include guard prevents duplicate definitions");
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 <stdio.h>
int main() {
printf("Headers should hold prototypes, not function bodies");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: