Common debugging patterns

In programming systems, one of the main uses of console output is debugging. C++ streams provide two different global objects—std::cout and std::cerr. The first option, std::cout, is typically buffered, sent to stdout, and only flushed when either std::flush or std::endl is sent to the stream. The second option, std::cerr, provides the same functionality as std::cout, but is sent to stderr instead of stdout, and is flushed on every call to the global object. Take a look at the following example:

#include <iostream>int main(){    std::cout << "buffered" << '\n';    std::cout << "buffer flushed" << std::endl;    std::cerr << "buffer flushed" << '\n';}> g++ -std=c++17 scratchpad.cpp; ./a.outbufferbuffer flushedbuffer flushed ...

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.