Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

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("");

OpenStudy (rsmith6559):

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.

OpenStudy (farmdawgnation):

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.

OpenStudy (farmdawgnation):

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.)

OpenStudy (anonymous):

@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 :(

OpenStudy (anonymous):

stream insertion operators*

OpenStudy (anonymous):

http://www.cplusplus.com/reference/iostream/ostream/ At the very bottom, they have some member functions for the ostream stuff.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!