Difference between cout and puts() in C++ with Examples
Standard Output Stream(cout): The C++ cout statement is the instance of the ostream class. It is used to display output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<). For more details prefer to this article.
puts(): It can be used for printing a string. It is generally less expensive, and if the string has formatting characters like ‘%’, then printf() would give unexpected results. If string str is a user input string, then use of printf() might cause security issues. For more details prefer to this article.
|1||It is a predefine object of ostream class.||puts is a predefine function (library function).|
|2||cout is an object it uses overloaded insertion (<<) operator function to print data.||puts is complete function, it does not use concept of overloading.|
|3||cout can print number and string both.||puts can only print string.|
|4||to use cout we need to include iostream.h header file.||To use puts we need to include stdio.h header file.|
Program 2: Below program do not require fflush to flush the output buffer, because cout has it inbuilt.