17

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?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Kio Marv
  • 337
  • 2
  • 3
  • 8
  • [This](http://en.wikipedia.org/wiki/Standard_streams) might help. – devnull Oct 23 '13 at 11:32
  • @devnull Thanks for the link. But, `Unless redirected, input is expected from the keyboard which started the program`. That `redirected`... it means that the input can be from a file? – Kio Marv Oct 23 '13 at 11:34
  • In C *standard input* is `stdin` and *standard output* is `stdout`, both defined in ``; the fact that they are "keyboard" or "file" is irrelevant... – Basile Starynkevitch Oct 23 '13 at 11:37
  • @BasileStarynkevitch, but shoulend't they be treated different? If the input is from a file shouldn't it be opened first? – Kio Marv Oct 23 '13 at 11:57
  • possible duplicate of [confused about stdin, stdout and stderr?](http://stackoverflow.com/questions/3385201/confused-about-stdin-stdout-and-stderr) – Joe Oct 23 '13 at 18:39

5 Answers5

14

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • `At program startup, three text streams are predefined and need not be opened explicitly` OK. So whether it's a file or a keyboard input, I can read the `stdin` right away, without checking if it's opened, right? – Kio Marv Oct 23 '13 at 12:00
3

Yes, standard input (stdin) is input exepected from the keyboard. So, could be in the form of user input from a basic program or from a command line argument. Standard output (stdout) is the output of the code, usually to the terminal window. You could output your code almost anywhere, i.e. to a file, to a textbox, browser, but the standard is the stdout which is the terminal.

Hope that helps.

3

Normally, the standard input is the keyboard and the standard output the screen. However, you can redirect this in the command line using the "<" and ">" symbols. A command line like

dir /s > "Tree.txt" 

will change the standard output for the dir command to be the specified file. So all output goes to that file. The called application or command itself doesn't normally even notice the difference.

PMF
  • 14,535
  • 3
  • 23
  • 49
3

stdin is file descriptor 0, you can get a file to stdin by:

cat file |yourprog
#or
yourprog <file

likewise for stdout (file descriptor 1)

yourprog | someotherprog #pipe your stdout to the stdin of another program
yourprog > somefile #save stdout to a file
yourprog >> somefile #append stdout to a file

and stderr (fd 2)

yourprog 2> errlogfile

if you have a program that takes a file but doesn't handle stdin, you can use the above formats by doing this (assuming -f if the input file argument) myprog -f /dev/stdin

//and a horrible example of how not to read from stdin and write to stdout
char buf[4096];
while(write(1,buf,read(0,buf,4096)));
technosaurus
  • 7,676
  • 1
  • 30
  • 52
1

standard output (or stdout) refers to the standardized streams of data that are produced by command line programs (i.e., all-text mode programs) in Linux and other Unix-like operating systems.

Farouq Jouti
  • 1,657
  • 9
  • 15