Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means "standard input" in C language?

Tags:

c

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.

like image 373
Kio Marv Avatar asked Dec 26 '22 18:12

Kio Marv


2 Answers

"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).

like image 85
John Bode Avatar answered Jan 03 '23 21:01

John Bode


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

like image 45
ratchet freak Avatar answered Jan 03 '23 19:01

ratchet freak