# std::cerr, std::wcerr

> C++ Atlas: `cppatlas.com/w/cpp/io/cerr` | Source: https://en.cppreference.com/w/cpp/io/cerr | Domain: Input/output

The global objects std::cerr and std::wcerr control output to a stream buffer of implementation-defined type (derived from std::streambuf and std::wstreambuf, respectively), associated with the standard C error output stream stderr.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <iostream>
extern std::ostream cerr; // (1)
extern std::wostream wcerr; // (2)
```

The global objects `std::cerr` and `std::wcerr` control output to a stream buffer of implementation-defined type (derived from `std::streambuf` and `std::wstreambuf`, respectively), associated with the standard C error output stream `stderr`.

These objects are guaranteed to be initialized during or before the first time an object of type `std::ios_base::Init` is constructed and are available for use in the constructors and destructors of static objects with ordered initialization (as long as `<iostream>` is included before the object is defined).

Unless `std::ios_base::sync_with_stdio(false)` has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.

Once initialized, `(std::cerr.flags() & unitbuf) != 0` (same for `std::wcerr`) meaning that any output sent to these stream objects is immediately flushed to the OS (via `std::basic_ostream::sentry`'s destructor).

In addition, `std::cerr.tie()` returns `&std::cout` (same for `std::wcerr` and `std::wcout`), meaning that any output operation on `std::cerr` first executes `std::cout.flush()` (via `std::basic_ostream::sentry`'s constructor).

### Notes

The 'c' in the name refers to "character" (stroustrup.com FAQ); `cerr` means "character error (stream)" and `wcerr` means "wide character error (stream)".

### Example

Output to `stderr` via `std::cerr` flushes out the pending output on `std::cout`, while output to `stderr` via `std::clog` does not.

```cpp
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;

void f()
{
    std::cout << "Output from thread...";
    std::this_thread::sleep_for(2s);
    std::cout << "...thread calls flush()" << std::endl;
}

int main()
{
    std::jthread t1{f};
    std::this_thread::sleep_for(1000ms);
    std::clog << "This output from main is not tie()'d to cout\n";
    std::cerr << "This output is tie()'d to cout\n";
}
```

Possible output:

```text
This output from main is not tie()'d to cout
Output from thread...This output is tie()'d to cout
...thread calls flush()
```

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 455 | C++98 | `std::cerr.tie()` and `std::wcerr.tie()` returned null pointers | they return `&std::cout` and `&std::wcout` respectively |

### See also

- [`Init`](https://en.cppreference.com/w/cpp/io/ios_base/Init): initializes standard stream objects (public member class of `std::ios_base`)
- [`clog`](https://en.cppreference.com/w/cpp/io/clog), [`wclog`](https://en.cppreference.com/w/cpp/io/clog): writes to the standard C error stream `stderr` (global object)
- [`cout`](https://en.cppreference.com/w/cpp/io/cout), [`wcout`](https://en.cppreference.com/w/cpp/io/cout): writes to the standard C output stream `stdout` (global object)
- [`stdin`](https://en.cppreference.com/w/cpp/io/c/std_streams), [`stdout`](https://en.cppreference.com/w/cpp/io/c/std_streams), [`stderr`](https://en.cppreference.com/w/cpp/io/c/std_streams): expression of type `FILE*` associated with the input stream expression of type `FILE*` associated with the output stream expression of type `FILE*` associated with the error output stream (macro constant)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/cerr (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.