Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is “standard input”?

Tags:

c

stdout

stdin

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?

And the same goes for standard output: is it the console? a file?

like image 843
Kio Marv Avatar asked Oct 23 '13 11:10

Kio Marv


People also ask

What standard input means?

The standard input device, also referred to as stdin , is the device from which input to the system is taken. Typically this is the keyboard, but you can specify that input is to come from a serial port or a disk file, for example.

What is standard input in C?

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

What does standard input and output mean?

In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr).

What is the standard input Linux?

In Linux, stdin is the standard input stream. This accepts text as its input. Text output from the command to the shell is delivered via the stdout (standard out) stream. Error messages from the command are sent through the stderr (standard error) stream.


1 Answers

The C standard (e.g. C99 or C11) defines what should be expected from the standard <stdio.h> header (after having suitably #include-d it). See stdio(3) man page. Then you have the stdin and stdout and stderr file handles (pointers to some FILE which is an abstract data type).

The fact that stdin is related to some device (e.g. a keyboard) is implementation specific.

You could (but that would be unethical and/or inefficient) implement the C standard with e.g. a room of human slaves (that is unethical, if you use paid workers that would be just inefficient), instead of using a computer. Often, computers gives your some implementation of the C standard thru the help of some operating system.

You may want to know, inside your C program, if stdin is a "keyboard" or redirected from some "file". Unfortunately, AFAIK, there is no C99-standard way to know that.

As you mention, stdin, stdout and stderr should be available in your program at startup (i.e. after entering main ....). Hence, unless you fclose the stdin stream, you can read it (with getchar, scanf, getline, fgets, fscanf ... and friends) without any prior care (so you don't need to fopen it yourself).

On Linux or most Posix systems, you might use as an approximation isatty(STDIN_FILENO) - see isatty(3) for more - to test if stdin "is" the "keyboard" (by testing if it is some tty). See also this & that.

like image 165
Basile Starynkevitch Avatar answered Sep 20 '22 16:09

Basile Starynkevitch