This article analyzes the origin of standard input and standard output from the basic principle (the final conclusion is as follows ).
Some input/output operations, that is, I/O operations, are inevitable for a running program. for I/O operations, the C language provides a series of standard I/O functions to complete input and output tasks.
The main standard I/O functions include: fopen () (open a file), fprintf () (Write File), and fscanf () (read file ). to read and write a file, follow these steps:
Open the file and get the pointer to the file. Then, take the pointer as a parameter and call the corresponding Read and Write Functions to read and write the file. for example, if you want to obtain input from the keyboard (that is, to read a file, a program in UNIX can treat the keyboard and screen as normal files), you must first use fopen () to open the keyboard file, obtain the pointer to the file, and call fscanf () with the pointer as the parameter to read the file to obtain the keyboard input. If you want to output the information to the display (that is, to write the file ), you must first open the display file, obtain the pointer to it, and then call fprintf () to write information to the file using the pointer as the parameter to output information to the display. the preceding steps can be performed in a UNIX system using the following code:
/* In UNIX, read the keyboard input and input information to the screen. manually open the keyboard and display files and read and write them. */# include <stdio. h> int main (void) {file * PF; pF = fopen ("/dev/tty", "A +"); // terminal character file char sbuf [100]; fprintf (PF, "Please input a string:"); fscanf (PF, "% s", sbuf); fprintf (PF, "the input string is: \ "% s \". \ n ", sbuf); Return 0 ;}
Note:/dev/tty is a terminal character file, which is an abstraction of the keyboard and display. If it is written to this file, the write content will be displayed on the display and read from this file, the input is obtained from the keyboard.
The above method is used to obtain the keyboard input, and the output to the screen will be system-related (because different systems have different keyboard abstraction methods ). to encapsulate the underlying implementation details of the keyboard and screen I/O operations, the C language requires the system to provide two pointers for each program. These two pointers point to the keyboard and screen respectively and name these two pointers as follows: stdin and stdout. these two pointers are the so-called standard input and standard output. in this case, the code for retrieving input information from the keyboard and outputting information to the screen can be written as follows:
/* In any system, read the keyboard input and input information to the screen. standard input and standard output pointer must be used explicitly. */# include <stdio. h> int main (void) {char sbuf [100]; fprintf (stdout, "Please input a string:"); fscanf (stdin, "% s", sbuf ); fprintf (stdout, "the input string is: \" % s \". \ n ", sbuf); Return 0 ;}
Unlike code 1, this code can run on any system that supports Standard C. it is a very common operation to obtain the input from the standard input and display the information to the standard output (almost every console program includes this operation ), therefore, the C language provides functions specifically used to obtain information from standard input and display information to standard output, that is, scanf () and printf (). at this time, the code for retrieving input from the keyboard and displaying information to the screen can be written as follows:
/* The Standard C program reads keyboard input and inputs information to the screen. the standard input and standard output pointer must be used implicitly. */# include <stdio. h> int main (void) {char sbuf [100]; printf ("Please input a string:"); scanf ("% s", sbuf ); printf ("the input string is: \" % s \". \ n ", sbuf); Return 0 ;}
We can see that when using scanf () and printf (), you do not need to provide file pointers to them because they know the required file pointers, the objects they operate on are the files pointed to by standard input and standard output.
Conclusion:
Standard input and standard output are file pointers provided by the operating system for C Programs. By default, they are directed to the keyboard and screen respectively. the standard input function scanf () reads information from the file pointed to by the standard input, and the standard output function printf () sends the output information to the file pointed to by the standard output.
Note: For standard input, the standard output does not have to point to the keyboard or screen, and user programs can change their points.
Date: 2011-08-01 18:52:37 CST