Explain the Type of I/O Operations Supported by the C Library

This C Tutorial explains various type of I/O operations which are supported by standard C Library.

There are three basic forms of data which I/O functions deal with. These are

1. Individual characters
2. Textual data or Text lines
3. Binary data

Different set of I/O functions process each different type of data. Different members in each family or set of functions perform the same basic operation but in a slightly different way. Functions in each family differs in where they obtain input from and where they write output to.

getchar and putchar family of functions deal with reading and writing individual Characters. getchar family has getchar, getc and fgetc functions. getchar reads from stdin while others two read from any stream including stdin. For ex. from a file or device etc. Similarly, putchar family has putchar, putc and fputc functions. putchar writes to stdout while others two write to any stream including stdout.

Text lines are formatted and unformatted. printf and scanf family of functions read and write formatted text lines. printf writes the formatted text to stdout while fprintf writes to any stream including stdout. sprintf function writes to string in memory. Similarly, scanf reads formatted text from stdin and fscanf reads from any stream including stdin. sscanf reads from string in memory.


Reading and/or writing unformatted text of lines is accomplished by gets and puts family of functions. gets reads from stdin while fgets reads from any stream including stdin. Simlarly, puts writes lines to stdout while fputs writes to any stream including stdout.

Binary read and write is accomplished by fread and fwrite functions.

Consider a simple C program below

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
/* lo_input.c -- program uses sscanf() to process input line by line */
/* Though fscanf() skips over whitespaces and even newlines entered
 * sscanf() doesn't
#include <stdio.h>
#include <stdlib.h>
#define BSIZE 100
void linebyline(FILE *);
int main(void)
    printf("**This program reads set of 4 integers in each input and"
           " process them.**\n");
    return 0;
void linebyline(FILE *input)
    char buffer[BSIZE];
    int a, b, c, d, e;
    /* let's read input line by line using sscanf() */
    printf("To terminate program, hit ENTER key on the blank line.\n");
    while (fgets(buffer, BSIZE, input) != NULL && buffer[0] != '\n') {
        if (sscanf(buffer, "%d %d %d %d %d", &a, &b, &c, &d, &e) != 4 ) {
            fprintf(stderr, "Bad input skipped: %s", buffer);
        /* process the values */
        a += 1;
        b += 1;
        c += 1;
        d += 1;
        printf("values become: a = %d, b = %d, c = %d, d = %d\n",
                  a, b, c, d);
        printf("To terminate program, hit ENTER key on the blank line.\n");
    printf("Thank you!\n");

Let’s give attention to output of the above program,

**This program reads set of 4 integers in each input and process them.**
To terminate program, hit ENTER key on the blank line.
Bad input skipped: 12
23 4 12 756 22
Bad input skipped: 23 4 12 756 22
23 45 67 89
values become: a = 24, b = 46, c = 68, d = 90
To terminate program, hit ENTER key on the blank line.
Thank you!

Notice that program reads integers in a buffer and sscanf() reads exactly 4 integers from the buffer and process them. sscanf() fails when integers in the buffer are less than or greater than 4.


Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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


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.