Skip to content
Related Articles

Related Articles

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.NOcoutputs()
1 It is a predefine object of ostream class.puts is a predefine function (library function).
2cout is an object it uses overloaded insertion (<<) operator function to print data.puts is complete function, it does not use concept of overloading.
3cout can print number and string both.puts can only print string.
4to use cout we need to include iostream.h header file.To use puts we need to include stdio.h header file.

Program 1:

C++




// C++ program use of puts
#include <iostream>
#include <stdio.h>
using namespace std;
  
// main code
int main()
{
    puts("Geeksforgeeks");
    fflush(stdout);
    return 0;
}
Output:
Geeksforgeeks

Program 2: Below program do not require fflush to flush the output buffer, because cout has it inbuilt.

C++




// C++ program use of cout
#include <iostream>
using namespace std;
  
// main code
int main()
{
    cout << "Geeksforgeeks" << endl;
  
    return 0;
}
Output:
Geeksforgeeks
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :