Turning Learners Into Developers
Codekilla
CODEKILLA
C Language 8 min

C stdio.h Functions List with Examples

Read on to explore c stdio.h functions list with examples — a beginner-friendly walkthrough by Codekilla.

Rahul Chaudhary Thu Apr 30 2026
What is stdio.h?

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.

Why It Matters
  • 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 toolprintf() 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
Core Output Functions

The output functions let you send data to the screen or files. The printf() family is your bread and butter for displaying information.

FunctionPurposeReturns
printf()Print formatted text to stdoutNumber of characters printed
fprintf()Print formatted text to a file streamNumber of characters printed
sprintf()Print formatted text to a string bufferNumber of characters written
puts()Print string with automatic newlineNon-negative on success
putchar()Print single characterCharacter 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;
}
Core Input Functions

Reading data from users or files requires the input function family. These functions parse formatted input and handle different data types.

FunctionPurposeReturns
scanf()Read formatted input from stdinNumber of items successfully read
fscanf()Read formatted input from file streamNumber of items successfully read
sscanf()Read formatted input from stringNumber of items successfully read
gets()DEPRECATED — unsafe, buffer overflow riskPointer to string
fgets()Read string from stream (safe)Pointer to string or NULL
getchar()Read single characterCharacter 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;
}
File Operations

Working with files requires opening, reading, writing, and closing streams. The FILE* pointer is your handle to any file resource.

FunctionPurposeMode Examples
fopen()Open file and return stream pointer"r", "w", "a", "rb", "wb"
fclose()Close file streamReturns 0 on success
fread()Read binary data from fileReturns items read
fwrite()Write binary data to fileReturns items written
feof()Check if end-of-file reachedNon-zero if EOF
ferror()Check for file errorsNon-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;
}
Character and String File I/O

When you need character-by-character or line-by-line file processing, these functions give you fine-grained control.

FunctionPurposeStream
fgetc()Read one character from fileAny FILE*
fputc()Write one character to fileAny FILE*
fgets()Read line from fileAny FILE*
fputs()Write string to file (no newline)Any FILE*
ungetc()Push character back to streamAny 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;
}
File Positioning Functions

You can move around within a file instead of just reading sequentially. These functions let you jump to specific positions.

FunctionPurposeReturns
fseek()Move file pointer to position0 on success
ftell()Get current file positionCurrent position or -1L
rewind()Move file pointer to beginningvoid
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;
}
Quick Cheat Sheet
NeedReach for
Print formatted text to screenprintf()
Read formatted input from keyboardscanf()
Read a safe string from userfgets(buffer, size, stdin)
Open a file for readingfopen("file.txt", "r")
Write formatted data to filefprintf(filePtr, "format", vars)
Check if file endedfeof(filePtr)
Jump to file positionfseek(filePtr, offset, SEEK_SET)
Copy files byte-by-bytefgetc() + fputc()
Common Mistakes
  • Forgetting the & operator in scanfscanf("%d", number) won't work; you need scanf("%d", &number) to pass the address
  • Using gets() instead of fgets()gets() has no bounds checking and causes buffer overflows; always use fgets(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, causing fgets() to read an empty line; clear the buffer between them
  • Wrong format specifiers — Using %d for a float or %f for 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.

// was this useful?
Did this article answer your question?
// C Language · published by Codekilla
// related articles

Keep Reading