C++ Basic Input/Output

Hi everyone, inside this article we will see about C++ Basic Input/Output. Here, we will see Header files, Standard Output Stream (cout), Standard Input Stream (cin), and Standard End Line (endl) in detail.

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

If bytes flow from main memory to device like printer, display screen, or a network connection, etc, this is called as Output Operation. Whereas, If bytes 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

In C++, the standard I/O library provides several header files for input and output operations:

  1. <iostream>: This header file provides support for the standard input and output streams, cin and cout.
  2. <fstream>: This header file provides support for file I/O operations, including reading from and writing to files.
  3. <sstream>: This header file provides support for string I/O operations, including reading from and writing to strings.
  4. <iomanip>: This header file provides support for I/O manipulators, which allow you to control the format of data being read from and written to streams.
  5. <cstdio>: This header file provides support for C-style I/O operations, including printf and scanf functions.

Standard Output Stream (cout)

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

The cout stream in C++ is part of the standard I/O library and is used for standard output operations, such as printing text to the console.

Here is an example of using the cout stream in C++:

#include <iostream>
using namespace std;

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

In this example, the << operator is used to write the string “Hello, World!” to the cout stream, and the endl manipulator is used to insert a newline character into the output stream. The cout stream automatically flushes its buffer to the console whenever a newline character is written or the buffer is full, so the output will appear immediately.

Output

Hello, World!

Standard Input Stream (cin)

The cin is a predefined object of istream class. It is connected with the standard input device, which is usually a keyboard. The cin is used in conjunction with stream extraction operator (>>) to read the input from a console.

The cin stream in C++ is part of the standard I/O library and is used for standard input operations, such as reading data from the console.

Here is an example of using the cin stream in C++:

#include <iostream>
using namespace std;

int main()
{
    int x;
    cout << "Enter an integer: ";
    cin >> x;
    cout << "You entered: " << x << endl;
    return 0;
}

In this example, the >> operator is used to read data from the cin stream into the integer variable x. The cout stream is used to prompt the user for input and display the result. When the program runs, it will print the message “Enter an integer: ” to the console and wait for the user to enter a value. When the user enters a value and presses the Enter key, the value will be read into the x variable and printed back to the console.

Output

Enter an integer: 100
You entered: 100

Standard End Line (endl)

The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.

The endl manipulator in C++ is part of the standard I/O library and is used to insert a newline character into an output stream, such as cout. The endl manipulator is often used to separate lines of output and make it easier to read.

Here is an example of using the endl manipulator in C++:

#include <iostream>
using namespace std;

int main()
{
    cout << "Line 1" << endl;
    cout << "Line 2" << endl;
    cout << "Line 3" << endl;
    return 0;
}

In this example, the cout stream is used to print three lines of text to the console, separated by the endl manipulator. When the program runs, it will print the following output:

Line 1
Line 2
Line 3

Note that the endl manipulator not only inserts a newline character into the output stream, but also flushes the buffer of the stream. This means that any output written to the stream so far will be immediately sent to the output device (e.g., the console).

We hope this article helped you to understand about C++ Basic Input/Output in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.