+ 1

Cout vs Printf

I came to know that both are not same. I was astonished. For the sololearn compiler if you are getting time limit exceeded error prefer using printf it is faster than cout.

26th Nov 2016, 4:53 AM
Megatron
Megatron - avatar
2 Réponses
+ 1
Also from stackoverflow I get these differences. printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf()and scanf()): More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, <cstdio> uses "%" fields to figure out the types dynamically.Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors.Extensible: The C++ <iostream>  mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?!Inheritable: The C++ <iostream> mechanism is built from real classes such as  std::ostreamand  std::istream. Unlike <cstdio>'s FILE*, these are real classes and hence inheritable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.
26th Nov 2016, 4:55 AM
Megatron
Megatron - avatar
0
printf is slower than cout.
26th Nov 2016, 4:55 AM
Nguyễn Hoàng Long
Nguyễn Hoàng Long - avatar