[C] Streams – stdin, stdout, and stderr

In C, the “scanf()” function is used to read data from a keyboard and the “printf()” function writes data to a monitor. Actually, scanf and printf do not know nothing about a keyboard or a monitor. And then how do they work?

1. Streams

In C, all data flows are streams. When you read data from a file, it is called a file stream. Reading data from a keyboard or writing data to a monitor is also stream.


2. Standard Streams

To use streams, you need to open the target and connect to it.

C defines 3 types of standard streams by default. They are opened automatically when an application starts and are ready for use.

  • stdin: standard input (from a keyboard by default)
  • stdout: standard output (to a monitor by default)
  • stderr: standard error (to a monitor by default)

The “scanf()” is linked to the “stdin” stream. That’s all. It does not know or care where stdin came from. It happens to be a keyboard by default.

The “printf()” works the same way. It sends output data to stdout.


3. Redirecting stdin and stdout

In a command line window, you can easily redirect stdin and stdout to files using the redirect operators:

  • < : redirect “stdin” to another
  • > : redirect “stdout” to another
char name[100];
int i = 0;
while(scanf("%i, %100[^n]", &i, name) == 2)
  printf("Name = %s, Age = %i n", name, i);

Create a text file “simpsons.txt” in the same directory with the exe file:

36, Homer Simpson 10, Bart Simpson 9, Lisa Simpson

Run the code in a command line window

c:temp> ConsoleApp < “simpsons.txt”

It will read data from the file and shows on the monitor.

Now let’s redirect stdout to a file.

c:temp> ConsoleApp < “simpsons.txt” > “output.txt”


4. Stream Input

The “scanf()” function is a specialized version of the more generalized “fscanf()” function. All those functions are located in the “stdio.h“.

  • int fscanf(FILE* stream, const char* format, …);

FILE is a type to control a stream. The stream is not restricted as a physical file in a disk.

You can think of the “scanf()” is a shortcut function of “fscanf(stdin, …)“.


5. Stream Outtput

The “printf()” function is also a specific version of the “fprintf()” function.

  • int fprintf(FILE* stream, const char* format, …);


6. Standard Error Stream

Redirecting works as we expected. But what is “stderr” anyway?

Think about the situation when the “stdout” is redirected to a file. Look at the following example.

void StreamTest1()
  char name[100];
  int i = 0;
  while(scanf("%i, %100[^n]", &i, name) == 2)
    if (i >= 0)
      printf("Name = %s, Age = %i n", name, i);
      printf("Age cannot be less than zero. n");

When the error occurs and you print the message using “printf()“, the error message will be sent to the output file. In many cases, it is not what you want. You want to show error messages on screen.

The solution is to use “stderr” with “fprintf()“.

fprintf(stderr, "Age cannot be less than zero. n");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s