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

C #include

#include pastes the contents of another file into yours, like adding a chapter from another book. It is how you use libraries like stdio.h.
Syntax
c
#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

c
// 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;
}

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 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;
}

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 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;
}

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 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

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using angle brackets for your own header instead of quotes, so the compiler searches the system folders and does not find it.
  2. Forgetting include guards, which causes redefinition errors when a header is included twice.
  3. 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:

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.