Does a program that writes to "stdout" write to a file? the screen? I don't understand what it means to write to stdout.
asked May 7, 2013 at 23:13 1,460 3 3 gold badges 24 24 silver badges 39 39 bronze badgesIt means it writes to file descriptor 1. stdout is like a constant variable name, something that everyone uses so you don't have to remember that the actual FD number is 1. For example, when I first wrote this, I thought it was FD 0. :)
Commented Oct 6, 2015 at 16:38That means that you are printing output on the main output device for the session. whatever that may be. The user's console, a tty session, a file or who knows what. What that device may be varies depending on how the program is being run and from where.
The following command will write to the standard output device (stdout).
printf( "hello world\n" );
Which is just another way, in essence, of doing this.
fprintf( stdout, "hello world\n" );
In which case stdout is a pointer to a FILE stream that represents the default output device for the application. You could also use
fprintf( stderr, "that didn't go well\n" );
in which case you would be sending the output to the standard error output device for the application which may, or may not, be the same as stdout -- as with stdout , stderr is a pointer to a FILE stream representing the default output device for error messages.
answered May 7, 2013 at 23:14 K Scott Piel K Scott Piel 4,370 15 15 silver badges 19 19 bronze badgesIs there an example in C of the command to write to stdout? Is a simple printf statement write to stdout? What about writing to a file with write()?
Commented May 7, 2013 at 23:18printf by default writes on stdout , if you want to write to a specific stream you should use fprintf which accepts a FILE* as the destination stream.
Commented May 7, 2013 at 23:21Also it's "std" out because it's called "standard" output. As opposed to stdin or "standard input", stderr for "standard" error.
Commented May 7, 2013 at 23:39I wrote an addendum on how to force a flush of the buffer with fflush(stdout) here: stackoverflow.com/a/48551850/4561887
Commented Jan 31, 2018 at 21:37When you commit to sending output to stdout , you're basically leaving it up to the user to decide where that output should go.
If you use printf(. ) (or the equivalent fprintf(stdout, . ) ), you're sending the output to stdout , but where that actually ends up can depend on how I invoke your program.
If I launch your program from my console like this, I'll see output on my console:
$ prog Hello, World! #
However, I might launch the program like this, producing no output on the console:
$ prog > hello.txt
but I would now have a file "hello.txt" with the text "Hello, World!" inside, thanks to the shell's redirection feature.
Who knows – I might even hook up some other device and the output could go there. The point is that when you decide to print to stdout (e.g. by using printf() ), then you won't exactly know where it will go until you see how the process is launched or used.