What is Stream in C Programming?

Question: What is Notion of Stream in C Programming?

Answer: We perform writing to and reading from some of the common devices, viz. floppy drives, CD-ROM drives, hard drives, network communication etc. Each of these devices have different characteristics and operating protocols. It’s the operating system that takes care of details of communication with these devices and provides with simpler and more uniform I/O interface to the programmer.

ANSI C abstracts all I/O as stream of bytes moving into and out of a C program. This stream of bytes is called STREAM. A C program is concerned only with creating the correct stream of bytes and interpreting the stream of bytes coming into the program as input. Mostly all I/O streams are fully buffered. This means that reading and writing data is actually copying data out of and into an area of memory called buffer. Because reading from and writing to memory is fast, this enhances the performance. Buffer for output stream is flushed, meaning physically written to device or file when it’s full. Writing full buffer is more efficient than writing data in small bits or pieces as program produces them. Similarly, input buffers are re-filled when they become empty by reading next large chunk of input from the device or file into the buffer.

The simple best strategy to debugging a C program that aborts abnormally is to sprinkle printf() statements throughout the program to identify the specific area where error occurred. Because of fully buffered I/O, the outputs of the calls to printf() are buffered and not showed up to the user when program aborts. This causes confusion to the user. In order to show up the outputs immediately before program aborts follow calls to fflush() with call to printf(). For ex.

/* fflush.c -- program explores fflush() */
#include <stdio.h>
#include <stdlib.h>
#define DEBUG fprintf(fp, "values: a = %d and b = %d\n", a, b)
#define BSIZE 50
#define SUM(a,b)   ((a) + (b))
int main(void)
    int a = 10, b = 20;
    int *pa = &a, *pb = &b;
    int *pi = (int *)1000;
    FILE *fp;
    fp = fopen("fflush_out.txt", "w+");
    if (fp == NULL) {
        perror("File Open");
    /* Program calls fflush() with DEBUG statements */
    /* fflush() forces the output buffer to be physically written */
    /* whether or not it's full */
    fprintf(fp, "Sum of %d and %d is %d\n", a, b, SUM(a,b));
    /* fushing the stdin is undefined */
    /* NULL to fflush() flushes all the output buffers */
    fprintf(fp, "What value pointer *pi is pointing at, %d\n", *pi);
    printf("Add. of a and b is %p and %p\n", pa, pb);
    return 0;

Output on the screen as

Segmentation fault (core dumped)

while output directed to file “fflush_out.txt” contains

values: a = 10 and b = 20
Sum of 10 and 20 is 30

Notice that above program aborts because of seg fault. But calls to fflush() with DEBUG statements causes the buffer to be physically written to the file identified by “fp” wherein DEBUG statements might help you trace the cause of abnormal program termination.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

If you find any mistake above, kindly email to [email protected]

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.