So I wonder whether printf() function in C has the same effect.
Both C and C++ uses streams for output. In C it is stdout
and in C++ it is cout
.
Though it is not evident from the statement printf
writes to standard output(stdout) say a terminal.
In case of cout, it is evident from a statement itself, where the output is going.
Some subtle differences
With cout
you might need to include an additional header - say iomanip - and use some functions - say setw() - to have fine formatting where as in printf
you rely on format string.
Performance - Each has its own advantage depending on what you print and where you print. I borrowed this point from here.
Another similarity
Both C++ and C standards mention nothing about the order of evaluation of function arguments. So you must not try fancy stuff with functions. For example neither should you do
printf(%d%d",++i,i++); // The behaviour is undefined.
nor should you do
cout<<++i<<++i; // The behaviour is undefined.
Note:
Remember that the c streams are available in C++ if you include the necessary headers.