C-style formatting
std::size_t value = 42;
printf("Value is: %zu\n", value);
long unsigned int value = 100000;
printf("Value is: %lu\n", value);
std::cout
- The
std::fixed
manipulator forces the output stream to display floating-point numbers in fixed-point notation.
std::size_t value = 42;
std::cout << "Value is: " << value << std::endl;
long unsigned int value = 100000;
std::cout << "Value is: " << value << std::endl;
double number = 123.456789;
/* The `std::setprecision(int n)` manipulator sets the number of digits to appear after the decimal point when used with `std::fixed`. In this example, exactly 3 digits will be displayed after the decimal point.
*/
std::cout << std::fixed << std::setprecision(3) << number << std::endl; // output 123.457
/* If you remove std::fixed and use std::setprecision(3) alone, the precision will apply to the total number of significant digits, which can result in scientific notation or fewer decimal places based on the number's magnitude.
*/
- ge
for (int i = 1; i <= 100; ++i) { // Clear the last output using '\r' and print new content // \r (Carriage Return): Moves the cursor to the beginning of the line without advancing to a new line. // std::flush: Ensures the output is sent to the terminal immediately. std::cout << "\rProgress: " << i << "%" << std::flush; // Simulate work std::this_thread::sleep_for(std::chrono::milliseconds(100)); }
std::format (C++20)
std::size_t value = 42;
std::cout << std::format("Value is: {}\n", value);
long unsigned int value = 100000;
std::cout << std::format("Value is: {}\n", value);