Basic Input Output
Basic Input Output
DSA
Tutorials R
Data Science
Web Dev
Basic Input/Output in C++
C++ comes with libraries that provide us many ways for performing input and output.
In C++ input and output is performed in the form of a sequence of bytes or more
commonly known as streams.
Input Stream: If the direction of flow of bytes is from a device(for example Keyboard)
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( display screen ) then this process is called output.
In C++ articles, these two keywords cout and cin are used very often for taking inputs
and printing outputs. These two are the most basic methods of taking input and output
in C++. For using cin and cout we must include the header file iostream in our program.
In this article, we will mainly discuss the objects defined in the header file iostream like
cin and cout.
Standard output stream (cout): Usually the standard output device is the display
screen. cout is the instance of the ostream class. cout is used to produce output
on the standard output device which is usually the display screen. The data
needed to be displayed on the screen is inserted in the standard output stream
(cout)
We use cookies
Articles Read usingyou
to ensure thehave
insertion
the bestoperator (<<).
Prev on our website. By using our site, you
browsing experience Got
3 of 24 Complete. (13%) it!
CPP that you have read and understoodNext
acknowledge ourCookie Policy & Privacy Policy
https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 1/4
1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
#include <iostream>
All
Articles
int main( ) {
char sample[] = "GeeksforGeeks";
Videos
cout << sample << " - A computer science portal for geeks";
return 0;
Problems
}
Output: Quiz
As you can see in the above program the insertion operator(<<) insert the value
of the string variable sample followed by the string "A computer science portal
for geeks" in the standard output stream cout which is then displayed on screen.
standard input stream (cin): Usually the input device is the keyboard. cin is the
instance of the class istream and is used to read input from the standard input
device which is usually the keyboard. The extraction operator(>>) is used along
with the object cin for reading inputs. The extraction operator extracts the data
from the object cin which is entered using the keboard.
CPP
#include<iostream>
using namespace std;
int main()
{
int age;
Menu
cout << "Enter your age:";
cin >> age;
We use cookies to ensure
Articles Read
coutyou
<<have the bestage
"\nYour browsing experience
is: "<<age; on our website. By using our site, you
3 of 24 Complete. (13%)
acknowledge that you have read and understood ourCookie Policy & Privacy Policy
https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 2/4
1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
return 0;
}
All
Output:
The above program asks the user to input the age. The object cin is connected to
the input device. The age enteredProblems
by the user is extracted from cin using the
extraction operator(>>) and the extracted data is then stored in the variable age
present on the right side of the extraction operator.
Un-buffered standard error streamQuiz (cerr): cerr is the standard error stream
which is used to output the errors. This is also an instance of the ostream class. As
cerr is un-buffered so it is used when we need to display the error message
immediately. It does not have any buffer to store the error message and display
later.
CPP
#include <iostream>
int main( )
{
cerr << "An error occured";
return 0;
}
Output:
Menu
An error occured
Articles
#include <iostream>
int main( )
{
clog << "An error occured";Problems
return 0;
} Quiz
Output:
An error occured
Marked as Read
Report An Issue
If you are facing any issue on this page. Please let us know.
Menu
https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 4/4