C stdio.h Functions List with Examples
Read on to explore c stdio.h functions list with examples — a beginner-friendly walkthrough by Codekilla.
The stdio.h header file is the standard input/output library in C programming. It's one of the most fundamental header files you'll use, providing functions to read from and write to files, handle keyboard input, print to the screen, and manage data streams. When you include #include <stdio.h> at the top of your C program, you unlock dozens of functions that make communicating with users and handling data possible. Without it, you couldn't even print "Hello, World!" to the console.
Think of stdio.h as the communication bridge between your program and the outside world. Whether you're building a simple calculator or a complex file-processing system, you'll rely on these functions to move data in and out of your application.
- Universal I/O operations — Every C program that interacts with users or files depends on stdio.h functions
- File handling backbone — Reading from and writing to files is essential for data persistence and processing
- Debugging tool —
printf()is the most common debugging technique used by C programmers worldwide - Performance control — Understanding buffered vs unbuffered I/O helps you write faster, more efficient code
- System programming foundation — Operating systems, compilers, and embedded systems all use these low-level I/O primitives
The output functions let you send data to the screen or files. The printf() family is your bread and butter for displaying information.
| Function | Purpose | Returns |
|---|---|---|
printf() | Print formatted text to stdout | Number of characters printed |
fprintf() | Print formatted text to a file stream | Number of characters printed |
sprintf() | Print formatted text to a string buffer | Number of characters written |
puts() | Print string with automatic newline | Non-negative on success |
putchar() | Print single character | Character printed (as int) |
c#include <stdio.h> int main() { int age = 25; float price = 19.99; char grade = 'A'; // Basic formatted output printf("Age: %d, Price: $%.2f, Grade: %c\n", age, price, grade); // Writing to a string buffer char buffer[100]; sprintf(buffer, "User is %d years old", age); printf("%s\n", buffer); // Simple string output with newline puts("This automatically adds a newline!"); // Character output putchar('X'); putchar('\n'); return 0; }
Reading data from users or files requires the input function family. These functions parse formatted input and handle different data types.
| Function | Purpose | Returns |
|---|---|---|
scanf() | Read formatted input from stdin | Number of items successfully read |
fscanf() | Read formatted input from file stream | Number of items successfully read |
sscanf() | Read formatted input from string | Number of items successfully read |
gets() | DEPRECATED — unsafe, buffer overflow risk | Pointer to string |
fgets() | Read string from stream (safe) | Pointer to string or NULL |
getchar() | Read single character | Character read (as int) |
c#include <stdio.h> int main() { int number; char name[50]; float temperature; // Reading formatted input printf("Enter your age: "); scanf("%d", &number); // Clear input buffer before fgets while (getchar() != '\n'); // Safe string input with size limit printf("Enter your name: "); fgets(name, sizeof(name), stdin); // Reading from a string char data[] = "Temperature: 98.6"; sscanf(data, "Temperature: %f", &temperature); printf("Parsed temperature: %.1f\n", temperature); return 0; }
Working with files requires opening, reading, writing, and closing streams. The FILE* pointer is your handle to any file resource.
| Function | Purpose | Mode Examples |
|---|---|---|
fopen() | Open file and return stream pointer | "r", "w", "a", "rb", "wb" |
fclose() | Close file stream | Returns 0 on success |
fread() | Read binary data from file | Returns items read |
fwrite() | Write binary data to file | Returns items written |
feof() | Check if end-of-file reached | Non-zero if EOF |
ferror() | Check for file errors | Non-zero if error occurred |
c#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char buffer[100]; // Writing to a file file = fopen("output.txt", "w"); if (file == NULL) { perror("Error opening file"); return 1; } fprintf(file, "Hello, File I/O!\n"); fprintf(file, "Line number: %d\n", 2); fclose(file); // Reading from a file file = fopen("output.txt", "r"); if (file != NULL) { while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); } fclose(file); } return 0; }
When you need character-by-character or line-by-line file processing, these functions give you fine-grained control.
| Function | Purpose | Stream |
|---|---|---|
fgetc() | Read one character from file | Any FILE* |
fputc() | Write one character to file | Any FILE* |
fgets() | Read line from file | Any FILE* |
fputs() | Write string to file (no newline) | Any FILE* |
ungetc() | Push character back to stream | Any FILE* |
c#include <stdio.h> int main() { FILE *input, *output; int ch; input = fopen("source.txt", "r"); output = fopen("destination.txt", "w"); if (input == NULL || output == NULL) { perror("File error"); return 1; } // Copy file character by character while ((ch = fgetc(input)) != EOF) { fputc(ch, output); } fclose(input); fclose(output); return 0; }
You can move around within a file instead of just reading sequentially. These functions let you jump to specific positions.
| Function | Purpose | Returns |
|---|---|---|
fseek() | Move file pointer to position | 0 on success |
ftell() | Get current file position | Current position or -1L |
rewind() | Move file pointer to beginning | void |
fgetpos() | Get current file position (complex) | 0 on success |
fsetpos() | Set file position (complex) | 0 on success |
c#include <stdio.h> int main() { FILE *file = fopen("data.bin", "rb"); if (file == NULL) return 1; // Get file size fseek(file, 0, SEEK_END); long fileSize = ftell(file); printf("File size: %ld bytes\n", fileSize); // Go back to beginning rewind(file); // Jump to middle of file fseek(file, fileSize / 2, SEEK_SET); printf("Position: %ld\n", ftell(file)); fclose(file); return 0; }
| Need | Reach for |
|---|---|
| Print formatted text to screen | printf() |
| Read formatted input from keyboard | scanf() |
| Read a safe string from user | fgets(buffer, size, stdin) |
| Open a file for reading | fopen("file.txt", "r") |
| Write formatted data to file | fprintf(filePtr, "format", vars) |
| Check if file ended | feof(filePtr) |
| Jump to file position | fseek(filePtr, offset, SEEK_SET) |
| Copy files byte-by-byte | fgetc() + fputc() |
- Forgetting the
&operator in scanf —scanf("%d", number)won't work; you needscanf("%d", &number)to pass the address - Using gets() instead of fgets() —
gets()has no bounds checking and causes buffer overflows; always usefgets(buffer, sizeof(buffer), stdin) - Not checking if fopen() returned NULL — Files fail to open for many reasons (permissions, missing file); always validate the FILE pointer before using it
- Forgetting to fclose() files — Open files consume system resources; failing to close them leads to resource leaks and data corruption
- Mixing scanf() with fgets() —
scanf()leaves the newline character in the buffer, causingfgets()to read an empty line; clear the buffer between them - Wrong format specifiers — Using
%dfor a float or%ffor an int causes undefined behavior; match specifiers to variable types exactly
💡 Think Like a Programmer: Every I/O operation can fail, so defensive programming means checking return values and handling errors gracefully. The difference between a crashing program and a robust one is how you handle file errors and invalid input.
Keep Reading
C Preprocessor Directives Explained with Examples
Read on to explore c preprocessor directives explained with examples — a beginner-friendly walkthrough by Codekilla.
All C Language Keywords Explained with Examples
Read on to explore all c language keywords explained with examples — a beginner-friendly walkthrough by Codekilla.
All C Math Functions with Examples and Outputs
Read on to explore all c math functions with examples and outputs — a beginner-friendly walkthrough by Codekilla.
