Let\'s consider the following hello world examples in C and C++:
main.c
#include
int main()
{
printf(\"Hello world\\n\");
r
In addition to what all the other answers have said,
there's also the fact that std::endl is not the same as '\n'.
This is an unfortunately common misconception. std::endl does not mean "new line",
it means "print new line and then flush the stream".
Flushing is not cheap!
Completely ignoring the differences between printf and std::cout for a moment, to be functionally eqvuialent to your C example, your C++ example ought to look like this:
#include
int main()
{
std::cout << "Hello world\n";
return 0;
}
And here's an example of what your examples should be like if you include flushing.
C
#include
int main()
{
printf("Hello world\n");
fflush(stdout);
return 0;
}
C++
#include
int main()
{
std::cout << "Hello world\n";
std::cout << std::flush;
return 0;
}
When comparing code, you should always be careful that you're comparing like for like and that you understand the implications of what your code is doing. Sometimes even the simplest examples are more complicated than some people realise.