C++ Basic Input/Output

C++ I/O operation is using the stream concept. Stream is that the sequence of bytes or flow of knowledge . It makes the performance fast.

If data flow from main memory to device like printer, display screen, or a network connection, etc, this is called as output operation.

If data flow from device like printer, display screen, or a network connection, etc to main memory, this is called as input operation.

I/O Library Header Files

Header File Function and Description
<iostream> It is wont to define the cout, cin and cerr objects, which corresponds to standard output stream, standard input stream and standard error stream, respectively.
<iomanip> It is wont to declare services useful for performing formatted I/O, like setprecision and setw.
<fstream> It is wont to declare services for user-controlled file processing.

Standard output stream (cout)

The cout may be a predefined object of ostream class. It is connected with the standard output device, which is typically a monitor screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console

Example: -

#include <iostream>
using namespace std;

int main() {
int age = 18;
cout << "Your Age = " age;
return 0;
}

Output:
Your Age = 18

Standard input stream (cin)

The cin may be a predefined object of istream class. It's connected with the standard data input device, which is typically a keyboard. The cin is employed in conjunction with stream extraction operator (>>) to read the input from a console.

Example: -

#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter Your Age: ";
cin >> age;
cout << "Your Age is " age;
return 0;
}

Output:
Enter Your Age: 18
Your Age is 18

Standard end line (endl)

The endl may be a predefined object of ostream class. It's wont to insert a new line characters and flushes the stream.

Example: -

#include <iostream>
using namespace std;

int main() {
cout << "Welcome to Program Studio"<< endl;
cout << "C++ Tutorials"<< endl;
cout << "and Examples";
return 0;
}

Output:
Welcome to Program Studio
C++ Tutorials and Examples