What are the differences between puts and cout in C++

What are the differences between puts and cout in C++:

C++ puts and cout both are used to print values to the console. If you are from the C background, you might have use cout in C programming before. But both are not the same. Let’s take a look at the differences between puts and cout in C++:

Difference between puts and cout in C++:

  1. puts is defined in stdio.h header file and cout is defined in iostream.h header file. We need to import the file before using any of these.
  2. puts is a predefined library function and cout is a predefined object.
  3. Overloaded insertion operator, << is used with cout to print data. But puts is a function and we don’t have to overload for this.
  4. puts can print only string but cout can print both string and numbers.
  5. cout flushes the output but puts doesn’t flush automatically. We need to use fflush(stdout) to flush the buffer.

Example of puts and cout:

The below program shows how to use puts and cout:

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    cout<<"Hello World"<<" !!"<<endl;
    puts("Hello World !!");
}

It will print:

Hello World !!
Hello World !!

You might also like: