← Back to C Course | Chapter 11: Preprocessor | Lesson 3 of 5

C #include

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

c
#include <stdio.h>
int main() {
	printf("Uses angle brackets for system headers");
	return 0;
}

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

c
#include <stdio.h>
int main() {
	printf("Custom headers use double quotes");
	return 0;
}

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

c
#include <stdio.h>
int main() {
	printf("stdio.h itself may include other headers");
	return 0;
}

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

c
#include <stdio.h>
#ifndef MYHEADER_H
#define MYHEADER_H
int main() {
	printf("Include guard prevents duplicate definitions");
	return 0;
}
#endif

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

c
#include <stdio.h>
int main() {
	printf("Headers should hold prototypes, not function bodies");
	return 0;
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.