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.
- Defined in the header file.
- Uses the insertion operator (<<) to send data to the output stream.
- Supports output of different data types, including strings, characters, numbers, and expressions.
- Can be combined with manipulators and member functions to control output formatting. C++ `
#include using namespace std;
int main() {
// Print standard output
// on the screen
cout << "Welcome to GFG";
return 0;}
`
**Explanation
- cout sends the string "Welcome to GFG" to the standard output stream.
- The insertion operator (<<) passes the data to cout.
- The text is displayed on the console when the program executes.
Syntax
cout << var_name;
**Where:
- **cout: It is the standard output stream object.
- ****<<**: It is the insertion operator used to insert data into cout.
- **var_name: It represents the variable or literal whose value you want to display.
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
- The string stored in s is displayed first using cout.
- The value of n is printed immediately after the string in the same output statement.
- The insertion operator (<<) is chained to display multiple values sequentially.
- The output produced is: The answer is 42
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
- cout.write(s, 6) prints the first 6 characters of the character array.
- cout.put(c) outputs the character stored in c.
- These functions provide more control over output than the insertion operator.
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
- cout.precision(5) sets the output precision to 5 significant digits.
- cout.precision(7) increases the precision to 7 significant digits.
- This function is useful when displaying floating-point values with controlled accuracy.