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++:
- 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.
- puts is a predefined library function and cout is a predefined object.
- Overloaded insertion operator, << is used with cout to print data. But puts is a function and we don’t have to overload for this.
- puts can print only string but cout can print both string and numbers.
- 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:
- C++ program to find student grades using if-else
- C++ program to find a value in a linkedList
- C++ program to print a triangle with 1 and 0
- C++ program to implement binary search
- C++ program to delete the middle node in a linked list
- C++ program to capitalize first and last character of each word in a string