Skip to content
Related Articles
Open in App
Not now

Related Articles

Output in C++

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 20 Dec, 2022
Improve Article
Save Article

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++




// C++ program to demonstrate cout
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Used to display the output
    // to the standard output device
    cout << "GeeksforGeeks!";
 
    return 0;
}


Output:

GeekforGeeks!

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++




// C++ program to demonstrate the
// endl manipulator
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
 
    // Stores the data in a
    // variable str
    char str[] = "Geeksforgeeks";
 
    // Print the output
    cout << " A computer science portal"
         << " for geeks - " << str;
 
    return 0;
}


Output:

A computer science portal for geeks - Geekforgeeks

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++




// C++ program to illustrate the
// above concept
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Name variable can store
    // upto 30 character
    char name[30];
 
    // Print the output(asking user
    // to enter city name)
    cout << "Please enter your city name: ";
 
    // Take input from user and store
    // in name variable
    cin >> name;
 
    // Print output string including
    // user input data
    cout << "Your city is: "
         << name << endl;
 
    return 0;
}


Input: Mumbai

Output: Mumbai

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!