#include
- C course
- Beginner
- Lesson 107/116
- 5 min
Learn to include header files with #include.
#include: including header files
The #include directive adds the content of a header file to the program. System headers use <>, your own files use "".
#include <stdio.h> /* system */
#include "utils.h" /* own */The system header <stdio.h> is searched in the compiler's standard path; "utils.h" is searched first in the project directory. This distinction tells the compiler where to look. <string.h> brings string functions, <stdlib.h> brings memory functions.
System vs custom header
System headers go inside <>; the compiler searches them in the standard directory. Headers you create go inside "".
#include <stdio.h> /* standard dir */
#include "myheader.h" /* project dir first */This distinction tells the compiler the search order. With "" the compiler first looks in the project directory; if not found, it falls back to standard directories. With <> it goes directly to standard directories. So always use "" for your own headers.
Practice Print string length with strlen
Include the <string.h> header. In main declare char name[] = "Alex"; compute its length with strlen and print it with printf using the %zu format.
Starter code
#include <stdio.h>
// Include the string.h header
int main(void) {
char name[] = "Alex";
// Print the length with strlen and printf
return 0;
}Sample solution
#include <string.h>
printf("%zu\n", strlen(name));Expected output
4
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match header to content
| Term | Meaning |
|---|---|
<stdio.h> |
printf, scanf |
<string.h> |
strlen, strcpy |
<stdlib.h> |
malloc, free |
<math.h> |
sqrt, pow |
2 What is the difference between <string.h> and "utils.h"?
- Both are the same
- No syntax difference
- <> system, "" your own file Correct answer
- Different compiler
Explanation: <> for system headers, "" for your own files.
3 Fill in the blank: #include directive
____ <stdio.h>
#include <stdio.h>
Explanation: #include <stdio.h> includes the standard I/O.