Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”.
cin/cout is faster than scanf/printf, key differnce among these.
Why is scanf faster than cin?
On a high level both of them are wrappers over theread() system call, just syntactic sugar. The only visible difference is that scanf() has to explicitly declare the input type, whereas cin has the redirection operation overloaded using templates. This does not seem like a good enough reason for a performance hit of 5x.
It turns out that iostream makes use of stdio‘s buffering system. So, cin wastes time synchronizing itself with the underlying C-library’s stdio buffer, so that calls to bothscanf()and cin can be interleaved.
The good thing is that libstdc++ provides an option to turn off synchronization of all the iostream standard streams with their corresponding standard C streams using
std::ios::sync_with_stdio(false);
same with cout and printf.
but in simple words, cin, cout uses extraction and insertion in c++ which are basically overloaded, hence another factor of slow speed.
I hope this answer your question of why one is preferred over another and they are basically the way to input data, and internally cin, cout is written using c stdio buffer library.