C++ cout

The cout object is used to display the output to the standard output device. It is defined in the iostream header file.

Example

#include <iostream>

using namespace std;

int main() {
  int a = 24;
	
// print variable cout << "Value of a is " << a;
return 0; } // Output: Value of a is 24

cout Syntax

The syntax of the cout object is:

cout << var_name;

Or

cout << "Some String";

Here,

  • << is the insertion operator
  • var_name is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc.

cout with Insertion Operator

The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output".

The cout object is used along with the insertion operator << in order to display a stream of characters. For example,

int var1 = 25, var2 = 50;

cout << var1;
cout << "Some String";
cout << var2;

The << operator can be used more than once with a combination of variables, strings, and manipulators (like endl):

cout << var1 << "Some String" << var2 << endl;

Example 1: cout with Insertion Operator

#include <iostream>
using namespace std;

int main() {
  int a,b;
  string str = "Hello Programmers";
	
// single insertion operator cout << "Enter 2 numbers - ";
cin >> a >> b; cout << str; cout << endl;
// multiple insertion operators cout << "Value of a is " << a << endl << "Value of b is " << b;
return 0; }

Output

Enter 2 numbers - 6
17
Hello Programmers
Value of a is 6
Value of b is 17

cout with Member Functions

The cout object can also be used with other member functions such as put(), write(), etc. Some of the commonly used member functions are:

  • cout.put(char &ch): Displays the character stored by ch.
  • cout.write(char *str, int n): Displays the first n character reading from str.
  • cout.setf(option): Sets a given option. Commonly used options are left, right, scientific, fixed, etc.
  • cout.unsetf(option): Unsets a given option.
  • cout.precision(int n): Sets the decimal precision to n while displaying floating-point values. Same as cout << setprecision(n).

Example 2: cout with Member Function

#include <iostream>

using namespace std;

int main() {
  string str = "Do not interrupt me";
  char ch = 'm';
	
// use cout with write() cout.write(str,6);
cout << endl;
// use cout with put() cout.put(ch);
return 0; }

Output

Do not
m

cout Prototype

The prototype of cout as defined in the iostream header file is:

extern ostream cout;

The cout object in C++ is an object of class ostream. It is associated with the standard C output stream stdout.

The cout object is ensured to be initialized during or before the first time an object of type ios_base::Init is constructed. After the cout object is constructed, it is tied to cin which means that any input operation on cin executes cout.flush().


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges