C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language

Difference between cout and puts() in C++ programming language

Learn: What are the difference between cout and puts() in C++ programming language, what and when should be use them?

As we have learnt that both are used to print data on the console (output screen), but still they have some differences, in this post we are going to discuss about the differences between cout and puts() in C programming language.

cout Vs. puts() in C++

The basic differences are:

  1. cout is a predefine object of ostream class whereas puts is a predefine function (library function).
  2. cout is an object it uses overloaded insertion (<<) operator function to print data. But puts is complete function, it does not use concept of overloading.
  3. cout can print number and string both. Whereas puts can only print string.
  4. cout uses flush internally, whereas in case of puts does not, to flush stdout we have to use fflush function explicitly.
  5. To use puts we need to include stdio.h header file. While to use cout we need to include iostream.h header file.

Example of cout in C++

#include <iostream>
using namespace std;

int main()
{
	cout<<"Hello World"<<endl; 
	return 0;
}

Output

Hello World

This program do not require fflush to flush the output buffer, because cout has it inbuilt.

Example of puts() in C++

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	puts("Hello World\n"); 
	fflush(stdout);
	return 0;
}

Output

Hello World

Here, we should fflush(stdout) to flush the output buffer, since it is not require every time but sometimes (when data is printing frequently) it require to print data properly on real time.









COMMENTS