The cout object is an instance of the ostream class in C++ programming language. This class is defined within the < iostream > header file, where all identifiers are encapsulated within the std namespace. Therefore, the complete reference to cout is std::cout.
Basic Data Type Support
The cout stream supports multiple data types including integers, floating-point numbers, characters, and strings. These data types are automatically converted to string format during output operations.
#include <iostream>
using namespace std;
int main() {
int number = 1;
bool flag = true;
double value = 1.2;
char symbol = 'c';
cout << "number = " << number << endl
<< "flag = " << flag << endl
<< "symbol = " << symbol << endl
<< "value = " << value << endl;
return 0;
}
File Stream Integration
Output operations can be redirected to file stream objects, enabling content to be written directly to files. The statement std::cout << std::endl; flushes the output buffer, ensuring immediate display of output content.
Character Encoding Considerations
To prevent character encoding issues when displaying Chinese text, use std::wcout for wide character string output. Additionally, include the compilation flag -finput-charset=UTF-8 to specify the input file encoding format.
Numeric Base Conversion
By default, cout displays numbers in decimal format. Alternative numeric bases can be specified using various manipulators:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int num = 90;
cout << num << endl; // Default decimal
cout << dec << num << endl; // Decimal format (same as default)
cout << oct << num << endl; // Octal representation
cout << hex << num << endl; // Hexadecimal with lowercase letters
cout << setiosflags(ios::uppercase); // Enable uppercase letters
cout << hex << num << endl; // Hexadecimal with uppercase letters
cout << setbase(8) << num << endl; // Set base to 8 (octal)
return 0;
}
Precision Control
To control decimal precision, include the #include <iomanip> header. The expression cout << fixed << setprecision(2) << 1.23456 << endl; formats floating-point numbers to two decimal places.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(2) << 1.23456 << endl;
cout << setprecision(2) << fixed << 1.23456 << endl;
return 0;
}
The order of fixed and setprecision() does not affect the output result. C++ defaults to six significant digits for floating-point output. When integer digits exceed six, scientific notation is used. Precision can be modified through several methods:
- Using setprecision(n) sets the total significant digits for floating-point output
- Using setiosflags(ios::fixed) or fixed controls precision after the decimal point
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double sample = 123.456789;
cout << sample << endl;
cout << setprecision(3) << sample << endl;
cout << setprecision(9) << sample << endl;
cout << setiosflags(ios::fixed);
cout << sample << endl;
cout << fixed << setprecision(3) << sample << endl;
cout << setprecision(9) << fixed << sample << endl;
return 0;
}
Sign and Decimal Point Display
Use showpoint and showpos manipulators to control sign and decimal point visibility:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double positive = 5, negative = -1.2;
cout << positive << "\t\t" << negative << endl;
cout << showpoint << positive << "\t\t" << negative << endl;
cout << showpos << positive << "\t" << negative << endl;
return 0;
}
Field Width and Alignment
By default, output is right-aligned. Use setw() along with left or right manipulators:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int value = 10;
cout << setw(3) << value << endl; // Default right alignment
cout << setw(3) << left << value << endl; // Left alignment
cout << setw(3) << right << value << endl; // Right alignment
return 0;
}
Fill Character Specification
Use setfill() to specify fill characters for padding:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int hours = 14, minutes = 18, seconds = 10;
cout << setw(2) << setfill('0') << hours
<< ":" << minutes << ":" << seconds;
return 0;
}
| Manipulator | Description |
|---|---|
| setbase(n) | Outputs in base-n format (n=8,10,16) |
| setfill(ch) | Sets character padding, ch can be character constant or varible |
| setprecision(n) | Sets output precision to n digits |
| setw(n) | Sets field width to n characters, affects next output only |
| setiosflags(ios::uppercase) | Displays letters in uppercase |
| setiosflags(ios::fixed) | Controls digits after decimal point |
| setiosflags(ios::scientific) | Displays in scientific notation |
| setiosflags(ios::showpoint) | Forces decimal point display |
| setiosflags(ios::showpos) | Forces positive sign display |
| setiosflags(ios::left) | Sets left alignment |
| setiosflags(ios::right) | Sets right alignment |
| resetiosflags(…) | Terminates specified output formatting |