Skip to content
Related Articles

Related Articles

Output in C++
  • Last Updated : 01 Feb, 2021

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 << "GeekforGeeks!";
  
    return 0;
}
Output:
GeekforGeeks!

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[] = "Geekforgeeks";
  
    // Print the output
    cout << " A computer science portal"
         << " for geeks - " << str;
  
    return 0;
}
Output:
A computer science portal for geeks - Geekforgeeks

Program 3:

In this example, the user is 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 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:
Output:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :