What does it mean to write to stdout in C?
Asked Answered
S

7

59

Does a program that writes to "stdout" write to a file? the screen? I don't understand what it means to write to stdout.

Surrebuttal answered 7/5, 2013 at 23:13 Comment(1)
It 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... :)Surprising
S
72

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

Sedda answered 7/5, 2013 at 23:14 Comment(4)
Is 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()?Surrebuttal
printf 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.Firedog
Also it's "std" out because it's called "standard" output. As opposed to stdin or "standard input", stderr for "standard" error.Michel
I wrote an addendum on how to force a flush of the buffer with fflush(stdout) here: https://mcmap.net/q/327832/-what-does-it-mean-to-write-to-stdout-in-cEvensong
L
24

It depends.

When 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! # <-- output is here on my console

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.

Leman answered 2/6, 2015 at 5:17 Comment(0)
S
7

stdout is the standard output file stream. Obviously, it's first and default pointer to output is the screen, however you can point it to a file as desired!

Please read:

http://www.cplusplus.com/reference/cstdio/stdout/

C++ is very similar to C however, object oriented.

Sofia answered 8/5, 2013 at 0:2 Comment(0)
M
6

stdout is the standard output stream in UNIX. See http://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html#Standard-Streams. When running in a terminal, you will see data written to stdout in the terminal and you can redirect it as you choose.

Mauchi answered 7/5, 2013 at 23:18 Comment(0)
F
5

stdout stands for standard output stream and it is a stream which is made available to your program by the operating system itself. It is already available to your program from the beginning together with stdin and stderr.

What they point to (or from) can be anything. Actually, the stream just provides your program an object that can be used as an interface to send or retrieve data. By default it is usually the terminal but it can be redirected wherever you want: a file, to a pipe going to another process and so on.

Firedog answered 7/5, 2013 at 23:18 Comment(0)
E
4

@K Scott Piel wrote a great answer here, but I want to add one important point.

Note that the stdout stream is usually line-buffered, so to ensure the output is actually printed and not just left sitting in the buffer waiting to be written you must flush the buffer by either ending your printf statement with a \n

Ex:

printf("hello world\n");

or

printf("hello world"); 
printf("\n");

or similar, OR you must call fflush(stdout); after your printf call.

Ex:

printf("hello world"); 
fflush(stdout);

Read more here: Why does printf not flush after the call unless a newline is in the format string?

Evensong answered 31/1, 2018 at 21:36 Comment(0)
F
0

stdout is a term for the output stream. stdout is treated like a file, and if you write to it, you output whatever you write into it into the console.

Foran answered 24/4, 2023 at 1:36 Comment(3)
Are you sure that reading from stdout will behave as you have suggested? Or if it will even behave at all? See: https://mcmap.net/q/331046/-writing-to-stdin-and-reading-from-stdout-unix-linux-c-programmingKurzawa
@AdrianMole I'm not sure anymore. I'll look it up and get back to you.Foran
@AdrianMole Here: #18739739Foran

© 2022 - 2024 — McMap. All rights reserved.