For some output operations, you have to specify a file/stream to which the output is to be sent. The fprintf
function (from the C library) is an example of this. Its first argument is of type FILE*
, and it has to refer to a file that you've opened -- or to one of the default pre-opened files. The C++-specific std::cout << "hello\n"
is another example; std::cout
is a pre-opened output stream.
For other operations, such as printf
, the place where the output goes is implicit. printf(args...)
is defined to be equivalent to fprintf(stdout, args...)
.
The C stdout
(which is of type FILE*
) and the C++ std::cout
(which is of a type derived from std::basic_ostream
) both refer to the standard output. That's an output stream that's opened for you by the environment as your program starts executing.
The actual location where output sent to standard output goes depends on the operating system and on how you invoked your program. Typically it will be printed to the current terminal window by default. (On older systems it might have been a text-only terminal screen or a hard-copy terminal.) And most operating systems provide ways to redirect standard output, such as:
your_program > output.txt
or
your_program | another_program
or
your_program > /dev/null
These (attempt to) send the output to a specified file, to the input of another program, or to a device that discards all input sent to it.