As a rule, compilers cannot check arguments from printf, do not even count the number of arguments, and do not check whether they are suitable for a format string. They are “optimized” to do the job, but are a special case and may fail. Example:
printf("%s %d\n", 1, "two", 3);
This will compile (if the optimized compiler does not detect a failure), and at run time printf will consider the first argument (1) line and the second ("two") integer. printf won’t even notice that there is a third argument, and don’t notice if there aren’t enough arguments!
Using cout, the compiler must select a specific <<operator for each variable that you insert. Example:
cout<<1<<"two"<<3<<endl;
The compiler should change this on calls with the corresponding ostream&operator<<(int) and ostream&operator<<(const char*) (as well as ostream&operator<<(ios&(*)(ios&)) ).
cout will also be faster since format string interpretation is not performed.
source share