Disadvantages of turning off cin synchronization with C scanf

Viewed 209

I recently learned something from this question about cin in C++ and its speed was compared with the speed of scanf in C. Calling cin object is much slower than calling scanf function as usual but when I was reading the accepted answer, I understood if we call std::ios::sync_with_stdio(false);, cin synchronization with scanf is turned off and cin speed becomes so faster and even faster than scanf in this case.

So, will we face some problems if we turn it off? If so, what problems, and is it good to turn off cin synchronization with scanf in C? Thanks for helping.

2 Answers

If you use both sets of I/O functions (header <cstdio> or <stdio.h> and also <iostream>) on the same stream (e.g. the stdin stream is associated with both scanf and cin), then you'd better leave them synchronized.

If any one stream only uses one I/O family you can turn off synchronization (you can still use fscanf with a particular file and cin with stdin for example, as long as separate streams are involved).

Basically problem can manifest when C API is used at the same time. Many strange artifacts can appear: missing data, wrong order, unexpected error.

It is possible you have full control over full code stack and you can ensure that C API is not used.

Sadly usually your application uses third party libraries. One of them can use C API to output some data to stream (especially if one of them is C library). Since you do not control third party library behavior, synchronization is required.

Related