cout in C++ (original) (raw)

Last Updated : 4 Jun, 2026

cout is the standard output stream object in C++ used to display data on the screen. It is one of the most commonly used objects provided by the library for producing console output.

#include using namespace std;

int main() {

  // Print standard output
// on the screen
cout << "Welcome to GFG";

return 0;

}

`

**Explanation

Syntax

cout << var_name;

**Where:

Displaying Multiple Variables

In addition to displaying individual values, cout can output multiple variables and strings together in a single statement.

C++ `

#include using namespace std;

int main() { int n = 42; string s = "The answer is ";

  // Printing both n and s
cout << s << n;

return 0;

}

`

**Explanation

Commonly Used cout Member Functions

The cout object provides several member functions that help control the formatting and display of output.

Member Function Description
**cout.put(char) Writes a single character to the output stream.
**cout.write(char*, int) Writes a block of characters from the array to the output stream.
**cout.precision(int) Sets the decimal precision for displaying floating-point numbers.
**cout.setf(ios::fmtflags) Sets the format flags for the stream.
**cout.width(int) Sets the minimum field width for the next output.
**cout.fill(char) Sets the fill character for padding the field.

The following example demonstrates how to output individual characters and character arrays using cout.put() and cout.write().

C++ `

#include using namespace std;

int main() { char s[] = "Welcome at GFG"; char c = 'e';

// Print first 6 characters
cout.write(s, 6);

// Print the character c
cout.put(c);

return 0;

}

`

**Explanation

C++ program to illustrate the use of cout.precision():

C++ `

#include using namespace std;

int main() { double pi = 3.14159783;

// Set precision to 5
cout.precision(5);

cout << pi << endl;

// Set precision to 7
cout.precision(7);

cout << pi << endl;

return 0;

}

`

**Explanation