cin cout vs printf scanf

Why is scanf/printf preferred over cin/cout in C++ in case of large in put output?

This Might Help :slight_smile:

2 Likes

See this link on codechef, similar question is already answered here.

Scanf/printf is preferred because it is much faster than cin/cout which sometime might help avoiding TLE (Time Limit Exceeded).

You can read details here and here, also a google search will be helpful.

Also, getchar_unlocked() and other even faster methods exists, see it here and how it works here at stackoverflow.

A nice comparison of various I/O operations in C++ is given in this link => codeforces.

Note: Using faster I/O is not always sufficient (or important), you must always try to implement a fast algorithm to avoid TLE.

1 Like

Refer this. The article explains why scan, printf is faster than cin, cout and also the test part of the fact that why it is true.

Nope cin/cout is faster than printf/scanf bec in c++ its have buffer memory which holds the input/output, when its full it gives to output so that programmer preferred cin/cout not printf/scanf

have a look at this
why is cin/cout slower than printf/scanf

1 Like

@vishu25

I believe this is not true for cin() against scanf(), check this.

Maybe you are talking about std::ios_base::sync_with_stdio(false).

From personal experience,I have used both in a number of situations and found scanf to be faster than cin (although negligible, but may improve performance for very very large stdin input, but we handle them using file I/O).