0

You can specify the file to which values calulcated within some program are outputed using fout, but what about when you just use, for example, printf? Does this not print something on the command line (but then this is what cout does?) It seems to me that printf does print to a file, but to what file?

Just to note that I have seen the post 'printf' vs. 'cout' in C++ but the answers all seem to assume knowledge of what printf fundamentally does which is what I am having trouble understanding.

Meep
  • 351
  • 1
  • 4
  • 15
  • Possible duplicate of ['printf' vs. 'cout' in C++](https://stackoverflow.com/questions/2872543/printf-vs-cout-in-c) – Liora Haydont Jan 03 '18 at 20:39
  • 5
    This is not a programming question. This is a question about your operating system. In general, the standard output is not a physical file but a virtual file that represents a pipe, stream or device. – François Andrieux Jan 03 '18 at 20:39
  • it prints to `stdout`, whatever that happens to be when you run the program. – Kevin Jan 03 '18 at 20:40
  • 1
    *It seems to me that printf does print to a file, but to what file?* Incorrect. The `f` of `printf` stands for "formatted", not "file". `fprintf` prints to a file. `printf` prints to `stdout`, same as `std::cout`. See http://en.cppreference.com/w/cpp/io/c/fprintf. – R Sahu Jan 03 '18 at 20:41
  • The output destination of `printf` is platform dependent. Some embedded systems don't have output, so it doesn't go anywhere. Some windowing frameworks don't display the output because there needs to be a text window to display to. – Thomas Matthews Jan 03 '18 at 20:47
  • @FrançoisAndrieux: I disagree, it is a programming question. It's about where the output of a C++ program goes. – Keith Thompson Jan 03 '18 at 20:53
  • Have you *tried* something? – n. m. could be an AI Jan 03 '18 at 20:54
  • @KeithThompson I feel the answer to the question is beyond the scope of the language. It seems no different to me than if the question asked "where does user input come from?". It's given to you by the operating system. The answer to this question is entirely dependent on the operating system used and nearly language agnostic. – François Andrieux Jan 03 '18 at 20:58
  • 1
    @FrançoisAndrieux: it goes to *standard output*, and the C and C++ language standards have a fair amount to say about what that means. – Keith Thompson Jan 03 '18 at 21:00
  • 1
    After rereading the question, you may be confused by the assumption that `printf` and `cout` have distinct goals. They are both used to print to the standard output. `printf` is inherited by c++ from c where as `std::cout` was designed specifically for c++. – François Andrieux Jan 03 '18 at 21:05

2 Answers2

1

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.

Martin York
  • 257,169
  • 86
  • 333
  • 562
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

but what about when you just use, for example, printf?

printf outputs to the standard output stream.

Does this not print something on the command line

Possibly and typically yes. The shell controls where the standard output is streamed to. Unless the output is redirected, standard output is shown on the command line.

(but then this is what cout does?)

Yes. std::cout also prints to the standard output stream.


To understand what an input / output stream is, you can read about it from the Single UNIX Specification. UNIX is not part of C++ standard, but the C++ standard uses the same concept of standard streams.

A stream is associated with an external file (which may be a physical device) ...

...

At program startup, three streams are predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output) ...

but is not necessarily be a physical device, nor is it necessarily something that is stored on a disk.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326