Explain stdin, stdout, stderr in C language

When we write C programs, we often come across printf (), fprintf (), perror (), what exactly do these things do?I have to say stdin, stdout, stderr. Think about what we do when we write a File in C: File * FP =fopen(), which is what we request from the system, which is the equivalent of a channel to the File.
 
Actually, stdin,stdout,stderr is this FP, but it is turned on by default when the computer system is turned on, where 0 is stdin, which means input stream, which means input from the keyboard, 1 is STdout,2 is Stderr, and 1,2 is the monitor by default. While printf () prints out to stdout, which is equivalent to Fprintf (stdout, “***”), perror() prints out to Stderr, which is equivalent to Fprintf (stderr, “***”), what’s the difference between stdout and Stderr?
 
The reason we use printf () when we write a program is that we can monitor the health of our program, or debug, if our program is running all the time without stopping, we can’t keep staring at the screen to see the output of the program, then we can use file redirection. Will output to a file that we can look at later. For example, test.c
(CPP) view plain copy
1. & lt; The pre class = “CPP” name = “code” & gt; #include< stdio.h>   
2.
3. Int main ()
4. {
5. The printf (” stdout Helo World!!!!! \n”);    
6.
7. Return 0;   
8.}
After compiling, we./test > Test.txt (redirects the contents of stdout to a file by default) outputs the contents of the test program to a file called test.txt. There is a more explicit way of writing it. Test.txt, where the 1 stands for stdout. Speaking of which, you should know what Stderr should do. Test.c: That’s right.
(CPP) view plain copy
1. # include< stdio.h>   
2.
3. Int main ()
4. {
5. The printf (” Stdout Helo World!!!!! \n”);   
6. Fprintf (stdout, stdout “Hello World!! \n”);   
7. Perror (Stderr “Hello World!!!!! \n”);   
8. Fprintf (stderr, “stderr Hello World!! \n”);   
9.
10. Return 0;   
11.}
After compilation,./test, four outputs on the screen, if./test > Test.ext, the result is two Stderr Hello World output from the screen!! , Stdout Helo World!!!!! In the file test.txt, based on the above, it is easy to understand the current result, so we can do whatever we want with the output, such as:
 
./test 1> testout.txt 2> Testerr.txt, we’ll output stdout to testout.txt, stderr to testerr.txt;
./test 1> Testout.txt, output stdout to file testout.txt, output stderr to screen;
./test 2> Testerr.txt, output stderr to file testerr.txt, output stdout to the screen;
./test > test.txt 2> & 1, this is to redirect stdout and Stderr to the same file, test.txt.
 
If we don’t want to see the output, either on the screen or redirected to a file, don’t worry, Linux has a solution for everything,./test >/dev/zero 2 & gt; & 1, so you don’t see any output.
 
Note: An important difference between Stderr and Stdout is that Stderr is non-buffered and outputs immediately, whereas Stdout defaults to row buffering, which means it outputs when it hits’ \n ‘. If you want stdout to output in real time as well, add Fflush (stdout) to the end of the output statement to do so in real time.

Reprinted in WeChat embedded ARM public number

Read More: