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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



Geeksforgeeks

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program use of cout
#include <iostream>
using namespace std;
  
// main code
int main()
{
    cout << "Geeksforgeeks" << endl;
  
    return 0;
}

chevron_right


Output:

Geeksforgeeks

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :