In c++ what is the difference between std::cout
and std::wcout
?
They both control output to a stream buffer or print stuff to the console, or are they just alike ?
In c++ what is the difference between std::cout
and std::wcout
?
They both control output to a stream buffer or print stuff to the console, or are they just alike ?
They operate on different character types:
std::cout
uses char
as character typestd::wcout
uses wchar_t
as character typeOtherwise both streams write to standard output.
char
is used for narrow strings, suitable for 7-bit ASCII and 8-bit ANSI. wchar_t
is used for wide strings, aka Unicode strings. However, the size of wchar_t
is not portable, on some systems it is 16bit (suitable for UCS2/UTF-16), on others it is 32bit (suitable for UCS4/UTF-32). C++11 introduced new char16_t
and char32_t
types to deal with that descrepency. –
Ruthenious std::basic_streambuf
abstraction doesn’t handle chatacter encodings on this level. I don’t think there is defined synchronization between the narrow and wide character streams, i.e., alternately writing to each if them may cause blocks of characters being written, although with std::ios_base::sync_with_stdio(true)
(the default) the characters are still not buffered. –
Lefthand sync_with_stdio(false)
was called, I would expect characters to be emitted in blocks from std::cout
and std::wcout
. With sync_with_stdio(true)
the same may happen but I would guess that for most implementations things interleave the way they are written as sync_with_stdio(true)
typically disables buffering of the standard streams. –
Lefthand Another thing is that both are used with respected input stream.
Objects of these are initialized during or before the first time an object of std::ios_base::Init
is created.
std::cout
is std::basic_ios::tie
'd to std::cin
and to std::cerr
std:wcout
is std::basic_ios::tie
'd to std::wcin
and to std::wcerr
© 2022 - 2024 — McMap. All rights reserved.