Header files in C are files with a .h
extension that contain function declarations, macros, and constants. These files are included in a program using the #include
directive to access pre-defined functions and symbols.
Purpose of Header Files:
- Provide reusable code for common operations.
- Enable modular programming by separating declarations and definitions.
- Reduce redundancy and improve readability.
Common Header Files
1. <stdio.h>
Stands for: Standard Input/Output Header
Purpose: Contains functions for input/output operations like reading from the keyboard and writing to the screen or a file.
Common Functions:
Function | Description |
---|---|
printf() |
Prints formatted output to the console. |
scanf() |
Reads formatted input from the keyboard. |
getchar() |
Reads a single character from the keyboard. |
putchar() |
Writes a single character to the console. |
fopen() |
Opens a file. |
fclose() |
Closes a file. |
fgets() |
Reads a string from a file or console. |
fprintf() |
Writes formatted output to a file. |
2. <conio.h>
Stands for: Console Input/Output Header
Purpose: Contains functions for console input/output, mostly specific to MS-DOS systems. It is not part of the C standard library and is rarely used in modern development.
Common Functions:
Function | Description |
---|---|
clrscr() |
Clears the console screen. |
getch() |
Reads a single character without echoing. |
getche() |
Reads a single character and echoes it. |
textcolor() |
Changes text color in the console. |
gotoxy() |
Moves the cursor to a specified position. |
3. <string.h>
Stands for: String Header
Purpose: Contains functions for handling strings and performing operations like copying, concatenation, comparison, and searching.
Common Functions:
Function | Description |
---|---|
strlen() |
Returns the length of a string. |
strcpy() |
Copies one string to another. |
strcat() |
Concatenates two strings. |
strcmp() |
Compares two strings. |
strchr() |
Finds the first occurrence of a character. |
strstr() |
Finds the first occurrence of a substring. |
strrev() |
Reverses a string (not standard). |
Summary Table
Header File | Purpose | Common Functions |
---|---|---|
<stdio.h> |
Input/output operations. | printf() , scanf() , fopen() , etc. |
<conio.h> |
Console-based input/output (not standard). | getch() , clrscr() , gotoxy() , etc. |
<string.h> |
String manipulation and operations. | strlen() , strcpy() , strcmp() , etc. |
Key Notes:
<stdio.h>
: Essential for most C programs.<conio.h>
: Optional and non-standard; avoid in portable programs.<string.h>
: Useful for string operations, widely used in programs dealing with text.