Search Your Topic

config
Wednesday, 07 June 2017 13:04

C++ Basic Input/Output

Written by
Rate this item
(0 votes)

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.

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

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

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

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

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main( ) {  
  4.    char ary[] = "Welcome to Anish sir C++ tutorial";  
  5.    cout << "Value of ary is: " << ary << endl;  
  6. }  

Output:

Value of ary is: Welcome to Anish sir C++ tutorial

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.

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main( ) {  
  4.   int age;  
  5.    cout << "Enter your age: ";  
  6.    cin >> age;  
  7.    cout << "Your age is: " << age << endl;  
  8. }  

Output:

Enter your age: 22
Your age is: 22

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):

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main( ) {  
  4. cout << "C++ Tutorial";     
  5. cout << " Anish sir "<<endl;   
  6. cout << "End of line"<<endl;   
  7. }   

Output:

C++ Tutorial Anish sir
End of line

 

Read 43 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ Program C++ if-else »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.