I was tasked with creating a test program in c
that reads the contents of the standard input and then prints them.
But I have a little doubt: what is exactly standard input
?
Is it what I type in the keyboard? Is it a file I have to read?
Both of them?
Thanks.
"Standard input" refers to a specific input stream, which is tied to file descriptor 0. It's the stream from which scanf
, getchar
, gets
(which you should never use), etc., all read. Basically, any stdio input function that doesn't take a FILE *
as an argument is reading from standard input.
It's usually tied to your console, but can be redirected to read from a file or other device.
For example,
scanf( "%d", &someVal );
is equivalent to
fscanf( stdin, "%d", &someval );
Both functions read from standard input (stdin
).
it is what you type on the keyboard when you run the program from the command line
it is one of the 3 standard streams defined for a program
when you start the program on a command line you can type some text i the terminal and that text will be passed to the standard input stream of the program
the 2 other streams are the standard out which is displayed on the terminal, and the error stream which is to display error messages that should not be in the standard out
on most terminals you can redirect the streams to and from files like so:
myprog.exe < file_to_read.txt
where file_to_read.txt will be read and passed into the input input stream
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With