Types of I/O Operations Supported by the C Library

What is I/O in C?

I/O (Input/Output) refers to any operation that involves getting data into your program (input) or sending data out of it (output).

For example:

  • Input: Reading numbers from the keyboard.
  • Output: Displaying results on the screen.

The C Standard Library provides several functions to perform I/O through stdin, stdout, and stderr.

Standard Input and Output (Console I/O)

These functions handle reading from the keyboard and writing to the terminal.

Functions:

  • printf(), scanf()
  • getchar(), putchar()
  • gets() (unsafe, deprecated), puts()

Example:

#include <stdio.h>
 
int main() {
    char mcqs[50];
 
    printf("Enter your quiz topic: ");
    scanf("%s", mcqs);
 
    printf("Welcome to the %s MCQ test!\n", mcqs);
 
    return 0;
}

Output:

advertisement
Enter your quiz topic: C
Welcome to the C MCQ test!

The program prompts the user to enter a quiz topic and stores it in a character array named mcqs. It uses scanf() to take the input and then prints a welcome message including the entered topic. The %s format specifier is used to read and display the string.

Character I/O

These functions handle input/output one character at a time.

Free 30-Day C++ Certification Bootcamp is Live. Join Now!

Functions:

  • getchar(), putchar()
  • fgetc(), fputc()

Example:

#include <stdio.h>
 
int main() {
    char c;
 
    printf("Enter one character: ");
    c = getchar();
 
    printf("You entered: ");
    putchar(c);
    putchar('\n');
 
    return 0;
}

Output:

Enter one character: b
You entered: b

This program takes a single character input from the user using getchar() and then displays it using putchar(). First, it prompts the user with a message. After the character is entered, it prints “You entered:” followed by the same character. A newline is added using putchar(‘\n’) for cleaner output.

String I/O

These functions handle reading and writing strings.

Functions:

  • gets() (deprecated), puts()
  • fgets(), fputs()

Example:

#include <stdio.h>
 
int main() {
    char quiz[100];
 
    printf("Enter quiz name: ");
    fgets(quiz, sizeof(quiz), stdin);
 
    printf("You entered: ");
    fputs(quiz, stdout);
 
    return 0;
}

Output:

advertisement
Enter quiz name: Java
You entered: Java

This program reads a full line of text input from the user using fgets() and then displays it using fputs(). It prompts the user to enter a quiz name, stores the input in the quiz array, and then prints “You entered:” followed by the inputted text.

Formatted I/O

Used for formatted data reading and writing (e.g., integers, floats, etc.)

Functions:

  • printf(), scanf()
  • fprintf(), fscanf()
  • sprintf(), sscanf() (for strings)

Example:

#include <stdio.h>
 
int main() {
    int id;
    float score;
 
    printf("Enter student ID and score: ");
    scanf("%d %f", &id, &score);
 
    printf("ID: %d, Score: %.2f\n", id, score);
    return 0;
}

Output:

Enter student ID and score: 45
67
ID: 45, Score: 67.00

This program takes a student’s ID and score as input from the user. It uses scanf() to read an integer and a float, then prints them using printf() with formatted output showing the score up to two decimal places.

File I/O

Used for reading from and writing to files.

Functions:

  • fopen(), fclose()
  • fread(), fwrite()
  • fscanf(), fprintf()
  • fgetc(), fputc()
  • feof(), ferror()

Example:

#include <stdio.h>
 
int main() {
    FILE *fp = fopen("mcq.txt", "w");
    if (!fp) {
        perror("File Error");
        return 1;
    }
 
    fprintf(fp, "Sanfoundry MCQ File Example\n");
    fclose(fp);
    return 0;
}

This program creates a file named mcq.txt for writing. If the file cannot be opened, it prints an error using perror(). Then, it writes a line of text into the file using fprintf() and closes the file using fclose().

Binary I/O

Used to read/write data in binary format.

Functions:

  • fread(), fwrite()

Example:

#include <stdio.h>
 
int main() {
    int quiz[3] = {10, 20, 30};
    FILE *fp = fopen("data.bin", "wb");
 
    if (!fp) {
        perror("Binary File Error");
        return 1;
    }
 
    fwrite(quiz, sizeof(int), 3, fp);
    fclose(fp);
    return 0;
}

This C program writes an array of three integers to a binary file named data.bin. It first opens the file in write-binary mode using fopen(). If the file fails to open, it prints an error message. Then, it uses fwrite() to write all three integers at once and closes the file using fclose().

Error and Status Checking

These functions detect and handle I/O-related errors.

Functions:

  • feof(), ferror(), clearerr(), perror()

Example:

#include <stdio.h>
 
int main() {
    FILE *fp = fopen("nofile.txt", "r");
 
    if (!fp) {
        perror("File Open Error");
        return 1;
    }
 
    fclose(fp);
    return 0;
}

This C program tries to open a file named nofile.txt in read mode. If the file does not exist or cannot be opened, it displays an error message using perror(). If the file opens successfully, it simply closes the file with fclose() and ends the program.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.