In this article, we will discuss the very basic and most common I/O operations required for C++ programming. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. This is the most basic method for handling output in C++.
The cout is used very often for printing outputs, i.e., on the monitor. The predefined object cout is an instance of iostream class. For formatted output operations, cout is used together with the insertion operator, which is written as “<<” (i.e., two “less than” signs).
Program 1:
Below is the C++ program to demonstrate the use of cout object:
C++
#include <iostream>
using namespace std;
int main()
{
cout << "GeeksforGeeks!" ;
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Note:
- #include is known as a preprocessor directive, which is used to load files.
- iostream is a header file that contains functions for input/output operations (cin and cout).
Program 2:
Below is the C++ program to demonstrate a manipulator that can be used with the cout object:
C++
#include <iostream>
using namespace std;
int main()
{
char str[] = "Geeksforgeeks" ;
cout << " A computer science portal"
<< " for geeks - " << str;
return 0;
}
|
Output
A computer science portal for geeks - Geeksforgeeks
Time Complexity: O(1)
Auxiliary Space: O(1)
Program 3:
In this example, the user will be asked for his/her city name, when the user enters his/her city it will store the city name in the name variable. After that, the console will print the output string. Below is the program for the same:
C++
#include <iostream>
using namespace std;
int main()
{
char name[30];
cout << "Please enter your city name: " ;
cin >> name;
cout << "Your city is: "
<< name << endl;
return 0;
}
|
Input: Mumbai
Output: Mumbai
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
14 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...