From Standard I/O to System I/O
Understand how C’s high-level standard I/O functions are built on top of low-level system calls, and learn the key differences in buffering, control, and error handling between <stdio.h> and system-level I/O.
So far in this course, we have used C’s standard input and output functions such as printf(), scanf(), fopen(), fread(), and fprintf(). These functions are convenient, portable, and easy to use. However, they are not the lowest level at which input and output occur.
Underneath the functions declared in <stdio.h>, the operating system provides lower-level system calls that perform the actual data transfer. In this lesson, we examine how standard I/O relates to system I/O and understand the differences in buffering, error handling, and control.
Two layers of I/O in C
C programs typically interact with I/O at two different levels:
Standard I/O (stdio library) is the high-level, portable interface provided by C. It includes functions like
printf(),scanf(),fopen(), andfgets(). It is buffered, meaning data is temporarily stored in memory before being sent to the operating system, which improves performance and makes it easier to use.System I/O (system calls) is the low-level interface provided directly by the operating system. It includes functions like
open(),read(),write(), andclose(). It is typically unbuffered and gives more direct control over file descriptors and data transfer, but it is less portable and more complex.
In short, standard I/O is easier and portable, while system I/O is lower-level and OS-controlled. When you write, printf("Hello\n");, you are not directly writing to the terminal. Instead:
printf()formats the output.The data is placed into an internal buffer.
Eventually, the C library invokes a system call such as
write()to transfer the data to the operating system.
Standard I/O is therefore a wrapper around system calls.
Standard I/O: Buffered and portable
Standard I/O functions are declared in <stdio.h>. They operate on objects of type FILE *. Commonly used functions include fopen(), fclose(), fprintf(), fread(), fwrite(), and printf(). Let's see an example below: