.h extension that
contains function declarations (prototypes), macro definitions, constants,
structure declarations, typedefs, and other reusable definitions. Header files
are included in a C program using the #include preprocessor
directive.
Syntax
#include
Example:
#include
#include
#include
What Does a Header File Contain?
| Contains | Purpose |
|---|---|
| Function Declarations (Prototypes) | Declare functions before they are used. |
| Macro Definitions | Define symbolic constants using #define. |
| Constants | Provide predefined constant values. |
| Structure and Union Declarations | Allow common data structures to be shared. |
| Typedef Definitions | Create aliases for existing data types. |
Uses of Header Files
- Provide declarations of library functions.
- Promote code reusability.
- Reduce code duplication.
- Improve program organization.
- Allow multiple source files to share common declarations.
- Make large projects easier to maintain.
- Provide access to standard library functions.
Example 1: Using
#include
int main()
{
printf("Welcome to C Programming!");
return 0;
}
Output
Welcome to C Programming!
The header file provides the declaration of the
printf() function.
Example 2: Using
#include
#include
int main()
{
printf("%.2f", sqrt(81));
return 0;
}
Output
9.00
The header file provides the declaration of
mathematical functions such as sqrt(), pow(),
sin(), and cos().
with the GCC compiler, use the
-lm compiler switch.
Example 3: Using
#include
#include
int main()
{
char name[] = "Rumman";
printf("%lu", strlen(name));
return 0;
}
Output
6
The header file provides declarations for string
handling functions such as strlen(), strcpy(),
strcmp(), and strcat().
Common Header Files in C
| Header File | Purpose | Common Functions |
|---|---|---|
|
Standard Input/Output | printf(), scanf() |
|
Mathematical Functions | sqrt(), pow() |
|
String Handling | strlen(), strcpy() |
|
General Utility Functions | malloc(), free(), exit() |
|
Character Handling | toupper(), tolower() |
|
Date and Time | time(), clock() |
User-Defined Header Files vs Standard Header Files
| Feature | Standard Header File | User-Defined Header File |
|---|---|---|
| Written By | C Standard Library | Programmer |
| Include Syntax | #include |
#include "myheader.h" |
| Purpose | Provides predefined functions | Stores reusable project declarations |
Common Mistakes
Avoid These Mistakes
- Forgetting to include the required header file before using a library function.
- Confusing a function declaration with its definition.
- Using angle brackets
< >instead of double quotes for user-defined header files. - Including unnecessary header files.
- Assuming a header file contains the implementation of every function.
Best Practices
- Include only the header files that your program requires.
- Organize reusable declarations in custom header files.
- Use meaningful names for user-defined header files.
- Keep function declarations and implementations separate.
- Use include guards in user-defined header files to prevent multiple inclusion.
Prerequisites
Before Learning This Topic
- Basic understanding of C program structure.
- Knowledge of functions in C.
- Basic understanding of the C preprocessor and the
#includedirective.
Interview Questions
- What is a header file in C?
- Why are header files used in C programming?
- What is the difference between
#includeand#include "file.h"? - What information is typically stored in a header file?
- Name five commonly used header files in C and their purposes.
- Can a programmer create custom header files? Explain.
- What are include guards, and why are they important?
Key Takeaway
Header files are reusable files with the .h extension that
contain function declarations, macros, constants, and data type
definitions. They enable programmers to access standard library functions,
organize code efficiently, reduce duplication, and improve maintainability.
Both standard and user-defined header files play an important role in
developing modular and scalable C programs.