In C++, what is the difference between these pieces of code, and which one should be used? Which is fastest? std::cout << std::endl; std::cout << "\n"; std::cout << '\n'; fprintf(stdout, "\n"); printf("\n"); putchar('\n'); puts("");
I wouldn't worry too much about the speed, but of the choices, I would think that std::cout << std::endl would be the most versatile, portable (to other OSs) and localization setting friendly of the bunch.
I agree with rsmith's answer above. I will add that the bottom 4 function calls are from the C standard library and will work in C or C++. The cout calls only work in C++. Additionally, I think you'll be required to use one of the printf variants if you want to have very specific control over formatting of the numbers that you're sending out.
The printf variants allow you to specify how many decimal places, etc - while I don't believe you can do that with cout. (At least, I've never seen it done.)
@farmdawgnation You can place some fancy objects stuff somewhere between your cout and your output stuff and all those stream extraction operators... but I forgot since I've never learned C++ yet :(
stream insertion operators*
http://www.cplusplus.com/reference/iostream/ostream/ At the very bottom, they have some member functions for the ostream stuff.
Join our real-time social learning platform and learn together with your friends!