CPP cout, cin, endl

CPP provides predefined input/output objects and functions to control the flow of data from device to memory and from memory to device.

CPP cout:

CPP cout is an inbuilt library object of ostream class, which is used for output. The cout object is defined in iostream.h (header file) in CPP library. CPP cout is used along with the stream insertion operator (<<) to print a given statement or a variable on the console.

Syntax:

cout << statement;

Example:

#include <iostream.h>
#include<conio.h> 
void main()
{
clrscr();  
cout <<"Welcome to the world of Programming!";
getch(); 
}

Output:

Welcome to the world of Programming!

CPP cin:

CPP cin is an inbuilt library object of istream class, which is used for input. The cin object is defined in iostream.h (header file) in CPP library. CPP cin is used along with the stream extraction operator (>>) to read an input data from the console.

Syntax:

cin>> statement;

Example:

#include <iostream.h>
using namespace std; 
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << “You Entered:<< num; 
return 0;
}

Output:

Enter a number: 5
You Entered: 5

CPP endl:

CPP endl is an inbuilt library object of ostream class, which is used to insert a new line character and flushes the stream.

Syntax:

cout << statement << endl;

Example:

#include <iostream.h>
using namespace std; 
void main()
{
cout << "Hello";
cout << "World!” << endl ";
cout << “Bye.” << num; 
}

Output:

Hello World!
Bye.
Please follow and like us:
Content Protection by DMCA.com