Header files help organize code by keeping declarations separate from the main code. The include directive lets you reuse code easily by adding these declarations where needed.
0
0
Header files and include directive
Introduction
When you want to use functions or variables declared in another file.
When you want to share common constants or macros across multiple files.
When you want to keep your code clean and organized by separating declarations from definitions.
When you use standard library functions like printf or scanf.
When you want to avoid rewriting the same code in multiple places.
Syntax
C
#include <header_file> // or #include "header_file.h"
Use angle brackets <> for standard library headers.
Use double quotes "" for your own header files in the project.
Examples
This includes the standard input/output library so you can use functions like printf.
C
#include <stdio.h>
This includes a user-defined header file named myheader.h from your project folder.
C
#include "myheader.h"Sample Program
This program shows how a function can be declared (like in a header) and then defined later. The include directive is used here to bring in the standard input/output functions.
C
#include <stdio.h> // Function declaration in header style void greet(); int main() { greet(); return 0; } // Function definition void greet() { printf("Hello from the greet function!\n"); }
OutputSuccess
Important Notes
Header files usually contain declarations, not full function definitions.
Including the same header multiple times can cause errors; use include guards or #pragma once to prevent this.
The include directive literally copies the header file content into your source file before compiling.
Summary
Header files store declarations to share between source files.
#include adds header file content to your code.
Use <> for system headers and "" for your own headers.