C++ std::wcout














































C++ std::wcout




DESCRIPTION

The wcout is an object of the class wostream. It is used to display output to standard output devices.
It is present in the <iostream> header file.

We normally use 'std::cout' to display the output. The std::cout uses char as the character type while 
std::wcout uses wchar_t as its character type. The main difference between char and wchar_t is that 
char can hold only 255 values(narrow characters) while wchar_t can hold more than 255 value(wide characters). Char can be used to store ASCII character sets, 
while wchar_t  can be used to store Unicode UTF-16 character set.

Both usage of std::cout and std::wcout is same, but the main difference is that wcout can display many other Unicode characters other than ASCII.

The wcout object is generally used along with the insertion operator(<<) to display a stream of characters.

General syntax :

std::wcout << variable;
std::wcout << L"Hello, World!";

Here the L before the string represents that the string is of wchar_t type.

It can also be used along with some member functions:

  • wcout.put(wchar_t &ch): Displays the wide character stored by ch.
  • wcout.write(wchar_t *str, int n): Displays the first n character reading from str.
  • wcout.setf(option): Sets a given option. Commonly used options are left, right, scientific, fixed, etc.
  • wcout.unsetf(option): Unsets a given option.
  • wcout.precision(int n): Sets the decimal precision to n while displaying floating-point values.

Example code:


#include <iostream>
using namespace std;
int main()
{
wchar_t var1[] = L"Welcome to cppsecrets!";
wchar_t ch = '!';

wcout << L"Hello, fello programmer!";
wcout << endl;
wcout.write(str,22); //Displays 22 charachters
wcout << "\n";
wcout.put(ch);

return 0;
}

Comments