C++ Standard Input/Output
C++ standard input/output operations are performed to flow bytes stream from keyboard (input device) to main memory and from main memory to display screen (output device) respectively. The input/output are done using standard libraries provided by C++.
C++ I/O Library Header Files
Library | Description |
---|---|
<iostream> | This library defines cin, cout, cerr,and clog objects for input stream, output stream, un-buffered error stream, buffered error stream respectively. |
<fstream> | This library defines file declares services for user-controlled file processing. |
<iomapin> | This library declares services useful for performing formatted I/O such as setw and setprecision. |
C++ Standard Output Stream (cout)
C++ has predefined object cout. It is an instance of ostream class. The cout uses insertion or put to operator (<<) for printing output on screen like printf() function in C language. The multiple use of << in a single statement is called cascading.
1 2 3 4 5 6 7 8 |
#include <iostream> using namespace std; int main(){ cout<<"This is output statement."<<endl; cout<<10; } |
Output:
1 2 3 4 |
This is output statement. 10 |
C++ Standard Input Stream (cin)
C++ has predefined object cin. It is an instance of istream class. The cin uses extraction or get from operator (>>) to wait for user to provide input like scanf() function in C language. We can also cascade input operator >>.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> using namespace std; int main() { int value; cout << "Please enter integer value: "; cin >> value; cout << "Your value is: " << value << endl; } |
Output:
1 2 3 4 |
Please enter integer value: 10 Your value is: 10 |
Note: The endl keyword is used to move cursor to new line.