std::scanf vs std::cin

Which is to be prefered much in codechef?

scanf is faster than cin. But in some websites they say that if we are inputting numbers of data type like unsigned long long int or long long int or like that it is better to use cin. So if the data-type of input is small like integer or float or char etc you may use scanf(). Else it is good to use cin. But as i have told you scanf is faster than cin, so there is a slight chance that if we get TLE we can try with scanf() instead of cin.

Happy Coding

actually for most of the problems, scanf/cin doesn’t matter. But for some problems where fast I/O is to be used, then scanf is preferred. If you have any doubt’s you can go through this link

http://discuss.codechef.com/questions/2589/scanfprintf-vs-cincout

Yes, scanf/printf are faster than cin/cout. Since you are asking for codechef competitive programming questions where speed is inportant you can use scanf/printf. But from my personal experiance it doesn’t matter which one you use the part is right algorithm that you use, I use cin>> but never goat a TLE because of this.

One more difference between scanf and cin with respect to integer overflow is that scanf does not (necessarily) test for integer overflow; if the number read is too big, the result is undefined behaviour. It’s common that you’ll just find an incorrect value in the corresponding argument, but there are no guarantees.

cin, on the other hand, does check for integer overflow, and if it finds integer overflow, it stores the largest possible integer of the indicated type and puts the cin stream into failure state. Once the stream is in failure state, it stays there until the state is cleared; furthermore, any subsequent use of cin will do nothing if the stream is in failure state.

Look at this question on spoj - Candy3 You need to use scanf because size is not specified so cin terminates when we enter a very long value.

Hope it helped.
Happy Coding

1 Like

Time allocated time to solve the problem in C and C++ is always same (unlike for other languages like java or python, the time allocated is more). And you already know that scanf()/printf() is faster than cin/cout. So whenever there is a large number of inputs (like multiple arrays/strings/graphs etc.) or large number of outputs, Prefer to use scanf/printf. You can define your own int_reader or char_reader using getchar(). To use scanf() and printf() more easily you can also define macros :-

#define scan(a) scanf("%d", &a)
#define pint(a) printf("%d\n", a)

@Amit : We assume that the inputs are not something ‘out of bound’ of the limits which are provided and it will be given as stated in the I/O format of the question. So that wont be a problem when you use cin/cout

Actually C IO is not faster than c++ IO. http://www.quora.com/C++-programming-language/Is-cin-cout-slower-than-scanf-printf Read answer by Anders Kaseorg. I’ve tested this myself and cin, cout are actually faster if you use std::ios_base::sync_with_stdio(false).

1 Like

@lallu1 I have already given the question link where input size is not provided and I can give you 2 versions of my code where one got WA and another got AC, the difference was cin and scanf respectively. See that link.

Do you need two versions of code? i can give you the link if you want.

@Amit Yes I have seen the link and I understand it gets failed with cin, but on codechef, inputs are always given some range and they are not fired out of bound or something bogus.

@Bipin2: I would suggest you to read last answer from this and let me know what you infer from that…
Python faster than C++? How does this happen? - Stack Overflow

I dont think scanf is included in std namespace

I morely emphasized on its Standard… rather than its namespace… :slight_smile:

scanf is better.

@Yash: Jo aap kahe mere aaka… :slight_smile: