C stdio.h Library Reference
Complete reference for stdio.h - all printf, scanf, and file functions with prototypes, parameters, and examples.
What You Will Learn
- ✓Know all printf/scanf variants
- ✓Understand file operation functions
- ✓Use character I/O functions
- ✓Reference function prototypes quickly
01Introduction to stdio.h
📚 What is stdio.h?
<stdio.h> (Standard Input/Output) is the most commonly used header in C. It provides functions for console I/O, file handling, and formatted output.
📖 New to I/O? Start with the beginner-friendly Input/Output Tutorial first.
📋 Key Functions Overview
| Category | Functions |
|---|---|
| Output | printf, fprintf, sprintf, snprintf |
| Input | scanf, fscanf, sscanf |
| Character | getchar, putchar, getc, putc, fgetc, fputc |
| String | gets, puts, fgets, fputs |
| File | fopen, fclose, fread, fwrite, fseek, ftell |
02printf() Family - Formatted Output
printf() - Print to Console
printf("Name: %s, Age: %d\n", "Alice", 25);// Output: Name: Alice, Age: 25fprintf() - Print to File/Stream
FILE *fp = fopen("log.txt", "w");fprintf(fp, "Error code: %d\n", 404);fprintf(stderr, "Error occurred!\n"); // Print to error streamfclose(fp);sprintf() - Print to String
char buffer[100];sprintf(buffer, "Score: %d/%d", 85, 100);printf("%s\n", buffer); // Output: Score: 85/100snprintf() - Safe Print to String
char buffer[20];snprintf(buffer, sizeof(buffer), "Long text: %s", "This is a very long string");// Safe: Won't overflow buffer!✅ Recommended: Always use snprintf() over sprintf() to prevent buffer overflow!
03scanf() Family - Formatted Input
scanf() - Read from Console
int age;char name[50];printf("Enter name and age: ");scanf("%s %d", name, &age); // Note: & for non-arraysprintf("Hello %s, you are %d years old\n", name, age);fscanf() - Read from File
FILE *fp = fopen("data.txt", "r");int id;float price;fscanf(fp, "%d %f", &id, &price);fclose(fp);sscanf() - Read from String
char data[] = "John 25 85.5";char name[20];int age;float score;sscanf(data, "%s %d %f", name, &age, &score);// name="John", age=25, score=85.504Character I/O Functions
| Function | Prototype | Description |
|---|---|---|
| getchar() | int getchar(void) | Read one char from stdin |
| putchar() | int putchar(int c) | Write one char to stdout |
| fgetc() | int fgetc(FILE *stream) | Read char from file |
| fputc() | int fputc(int c, FILE *stream) | Write char to file |
| ungetc() | int ungetc(int c, FILE *stream) | Push char back to stream |
1#include <stdio.h>23int main() {4 // Read and echo characters until Enter5 printf("Type something: ");6 int ch;7 while ((ch = getchar()) != '\n') {8 putchar(ch); // Echo each character9 }10 putchar('\n');11 12 // File character I/O13 FILE *fp = fopen("test.txt", "r");14 if (fp) {15 while ((ch = fgetc(fp)) != EOF) {16 putchar(ch); // Print file contents17 }18 fclose(fp);19 }20 return 0;21}05String I/O Functions
fgets() - Safe String Input ⭐
char name[50];printf("Enter name: ");fgets(name, sizeof(name), stdin); // Safe: won't overflow!// Note: fgets includes the newline characterfputs() - Write String to Stream
fputs("Hello World!\n", stdout); // To consoleFILE *fp = fopen("out.txt", "w");fputs("Written to file", fp);fclose(fp);puts() - Simple String Output
puts("Hello World!"); // Automatically adds newline// Same as: printf("Hello World!\n");⚠️ Never Use gets()!
gets() is deprecated and dangerous — no buffer size limit causes overflow. Always use fgets() instead!
06File Operations
| Function | Prototype | Description |
|---|---|---|
| fopen() | FILE *fopen(const char *path, const char *mode) | Open file |
| fclose() | int fclose(FILE *stream) | Close file |
| fread() | size_t fread(void *ptr, size_t size, size_t n, FILE *s) | Read binary data |
| fwrite() | size_t fwrite(const void *ptr, size_t size, size_t n, FILE *s) | Write binary data |
| fseek() | int fseek(FILE *stream, long offset, int whence) | Move file position |
| ftell() | long ftell(FILE *stream) | Get current position |
| rewind() | void rewind(FILE *stream) | Go to file start |
| feof() | int feof(FILE *stream) | Check end of file |
| ferror() | int ferror(FILE *stream) | Check for error |
1#include <stdio.h>23int main() {4 // Write to file5 FILE *fp = fopen("data.txt", "w");6 if (fp == NULL) {7 perror("Error opening file");8 return 1;9 }10 fprintf(fp, "Hello, File!\n");11 fclose(fp);12 13 // Read from file14 fp = fopen("data.txt", "r");15 char buffer[100];16 while (fgets(buffer, sizeof(buffer), fp) != NULL) {17 printf("%s", buffer);18 }19 fclose(fp);20 21 return 0;22}07File Opening Modes
| Mode | Description | If File Exists | If Not Exists |
|---|---|---|---|
| "r" | Read | Open | Error (NULL) |
| "w" | Write | Truncate (empty) | Create |
| "a" | Append | Open at end | Create |
| "r+" | Read/Write | Open | Error |
| "w+" | Read/Write | Truncate | Create |
| "rb" | Read binary | Add 'b' for binary mode | |
08Standard Streams
stdinStandard Input (keyboard)
stdoutStandard Output (console)
stderrStandard Error (errors)
09Summary
🎯 Key Functions
Output:
printf, fprintf, sprintf, snprintf
Input:
scanf, fscanf, sscanf, fgets
Character:
getchar, putchar, fgetc, fputc
File:
fopen, fclose, fread, fwrite
08Next Steps
Explore more C standard libraries: