2

In C++, you can control (with cin.tie) whether the cin stream is tied to the cout stream (i.e. if cout will flush whenever you have to read from cin). By default, this is true and it is useful for interactive programs (if you output a promt and then expect input, the prompt is guaranteed to be flushed on screen). But it can make a program slower due to unnecessary flushes.

Is there an equivalent way to control the synchronization between stdin and stdout when using plain C and <stdio.h>? Is there any guarantee about the default behavior?

(I've noticed most compilers do seem to tie stdin with stdout, but I haven't seen anything about it in the standard.)

Nu Nunu
  • 376
  • 3
  • 14
  • Perhaps this is a dupilicate? https://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c – Ivan Rubinson Nov 12 '18 at 18:25
  • [freopen()](https://en.cppreference.com/w/c/io/freopen)? – Ivan Rubinson Nov 12 '18 at 18:26
  • @IvanRubinson ios::tie has nothing to do with re-routing and freopen ... please read the question carefully – Nu Nunu Nov 12 '18 at 18:57
  • In the area of `std::ios_base::sync_with_stdio(false);`? – Eljay Nov 12 '18 at 20:43
  • 1
    No, C does not have this generic facility--you have the options in the answer to the linked question. flush after output, use line buffering, or use unbuffered output. *(Marking as duplicate because if there is a more sophisticated answer that someone does come up with, it would be equally appropriate to add there as here...but I don't think there's anything else to add.)* – HostileFork says dont trust SE Nov 13 '18 at 02:46
  • RE: duplication closing... I agree the initial closer of your question as a duplicate made a mistake. But something to know is that if a user has high enough rep, the system just takes their one word on a dupe ([the "dupehammer" privilege](https://meta.stackoverflow.com/q/305429/)). It's not a perfect system, *but* there are thousands of questions being triaged...and on the off chance that it is wrong, it can be fixed up (as here) so it works. I'd argue you [**can** love StackOverflow](https://stackoverflow.com/posts/53267941/revisions), as it's able to improve where other mediums can't! – HostileFork says dont trust SE Nov 13 '18 at 04:50

0 Answers0