C++ Basic Input/Output Function

                   C++ Basic Input/Output Function 

{tocify} $title= {Table of Contents}

In C++ Programming, the console IO operations are performed using the header file iostream.h. This header file provides two objects cin and cout to perform input and output operations respectively. C++ comes with libraries that provide us with many ways for performing input and output. In C++ input and output are performed in the form of a sequence of bytes or more commonly known as streams. A stream is an entity which is used by program to read or write characters. C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data. It makes the performance fast. For example, screen, keyboard or a file are represented as streams in C++ Programming language. You do not need to know about details of the used entity or technical specification to work with it.

The following header files are commonly used for input and output in C++:

Input Stream:

If the direction of flow of bytes is from the device( for example, Keyboard, printer, display screen, or a network connection, etc. ) to the main memory then this process is called input.

Output Stream:

 If the direction of flow of bytes is opposite, i.e. from main memory to device( for example, display screen, printer, or a network connection, . ) then this process is called output.

Library Header files in the Input/Output operations 

Let us see the common header files used in C++ programming are:




Standard output stream (cout)

Usually the standard output device is the display screen. 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.

Let’s see the simple example of standard output stream (cout):

Program:


When the above code is compiled and executed, it produces the following result −

Output:


Standard input stream (cin)

Usually the input device in a computer is the keyboard. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. 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.

Let’s see the simple example of standard input stream (cin):

Program:


Output:


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.

Let’s see the simple example of standard end line (endl):

Program:

#include <iostream>  

using namespace std;  

int main( ) {  

cout << "C++ Tutorial";<<endl;   

cout << "End of line"<<endl;   

}   

Output:

C++ Tutorial  
End of line

 

Post a Comment (0)
Previous Post Next Post