C stdio.h Library
C stdio.h Library
The stdio.h library stands for Standard Input/Output in C. It provides functions for input, output, file handling, and formatting. Most C programs rely on it.
📌 Common stdio.h Functions
1️⃣ Input Functions
| Function | Description |
|---|---|
scanf() |
Reads formatted input from the console |
getchar() |
Reads a single character from the console |
gets() |
Reads a string from the console (unsafe, avoid in modern C) |
fgets() |
Reads a string from a file or stdin (safe alternative) |
Example:
2️⃣ Output Functions
| Function | Description |
|---|---|
printf() |
Prints formatted output to the console |
putchar() |
Prints a single character |
puts() |
Prints a string with newline |
Example:
3️⃣ File Handling Functions
| Function | Description |
|---|---|
fopen() |
Opens a file in a specific mode (r, w, a, etc.) |
fclose() |
Closes a file |
fprintf() |
Writes formatted output to a file |
fscanf() |
Reads formatted input from a file |
fgetc() |
Reads a single character from a file |
fputc() |
Writes a single character to a file |
fgets() |
Reads a string from a file |
fputs() |
Writes a string to a file |
feof() |
Checks end-of-file |
fflush() |
Flushes output buffer |
Example: Writing to a File
Example: Reading from a File
4️⃣ Other Useful Functions
| Function | Purpose |
|---|---|
remove() |
Deletes a file |
rename() |
Renames a file |
rewind() |
Moves file pointer to beginning |
ftell() |
Returns current position of file pointer |
fseek() |
Moves file pointer to specified location |
Example: Using fseek() and ftell()
📌 Summary Table
| Category | Functions |
|---|---|
| Console Input | scanf, getchar, fgets |
| Console Output | printf, putchar, puts |
| File Handling | fopen, fclose, fprintf, fscanf, fgetc, fputc, fgets, fputs, feof, fflush |
| File Utilities | remove, rename, rewind, ftell, fseek |
✅ Notes
-
Always check if file opened successfully.
-
Prefer
fgets()overgets()for safety. -
printfandscanfuse format specifiers (%d,%f,%c,%s). -
fflush(stdin)is non-standard, avoid it; better usegetchar()to clear input buffer.
