Difference between cout and puts() in C++ with Examples
Last Updated :
08 Jul, 2020
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.
Differences are:
S.NO |
cout |
puts() |
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 1:
C++
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
puts ( "Geeksforgeeks" );
fflush (stdout);
return 0;
}
|
Program 2: Below program do not require fflush to flush the output buffer, because cout has it inbuilt.
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Geeksforgeeks" << endl;
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...