Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I coded C++ program, sometime I mixed using printf() and cout stream.
is it a good practice?

I try to get advice from you gurus and experts and see if I need to change my habits.

What I have tried:

this way I did many times and feel it is not an issue for small testing examples...
Posted
Updated 15-Oct-20 14:24pm

Picking one or the other is OK - though cout is "template friendly", unlike printf - but mixing them is probably not a good idea, as you are using two different buffered systems to output to the same device, so unless the streams are manually flushed between the uses it's possible that the outputs could be "randomly mixed" to an extent.

To be honest, you should probably stick to cout for C++ code, and printf only for C. Mixing them is going to look odd unless you have a very good reason - and some people will assume you don't know what you are doing based on that alone.
 
Share this answer
 
Comments
Southmountain 15-Oct-20 18:49pm    
thanks OG!
I don not consider it a good practice, but that's just personal taste.
Performance, however, could be a issue. See, for instance C++ Core Guidelines: Improved Performance with Iostreams - ModernesCpp.com[^].
 
Share this answer
 
Comments
Southmountain 15-Oct-20 18:48pm    
thanks for the good link:)
CPallini 16-Oct-20 2:27am    
You are welcome.
Shao Voon Wong 15-Oct-20 23:36pm    
Good link!
CPallini 16-Oct-20 2:27am    
:-)
Quote:
when I coded C++ program, sometime I mixed using printf() and cout stream.
is it a good practice?

No, it is not good practice !
choose 1 and stick to choice, never mix them..
 
Share this answer
 
Comments
Southmountain 16-Oct-20 13:04pm    
thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900