Reputation: 1771
// update 1:
Code Blocks 16.01
GCC 4.9.2
Windows 10
I am trying to understand:
Program goal:
// end update 1
When I compile the following code:
#include <iostream>
int main(void)
{
std::cout << sizeof(wchar_t) << "\n";
for (wchar_t ch = u'\u0000'; ch <= u'\uffff'; ++ch)
std::wcout << " " << ch;
std::cout << "\n\n\n";
}
I got the following warning: "character constant too long for its type" on the line of the for statement.
I ran the program and I got this:
.
I searched the net and all what I could find is that wchar_t size is implementation defined, but even so, it is 2 bytes on my system. I think it is big enough.
Q1: Why I got the warning?
Q2: Why there was a few number of characters in the output? I expected thousands of them.
Upvotes: 0
Views: 1563
Reputation: 15154
The following might work more as intended, displaying every printable codepoint from U+0000 to U+FFFF as Unicode. Be sure to set your console font to a Unicode font.
#include <cstdlib>
#include <cwctype>
#include <locale>
#include <iostream>
#if _WIN32 || _WIN64
// Windows needs a little non-standard magic for this to work.
#include <io.h>
#include <fcntl.h>
#include <locale.h>
#endif
using std::wint_t;
using std::iswprint;
void init_locale(void)
// Does magic so that wcout can work.
{
#if _WIN32 || _WIN64
// Windows needs a little non-standard magic.
constexpr char cp_utf16le[] = ".1200";
setlocale( LC_ALL, cp_utf16le );
_setmode( _fileno(stdout), _O_WTEXT );
#else
// The correct locale name may vary by OS, e.g., "en_US.utf8".
constexpr char locale_name[] = "";
std::locale::global(std::locale(locale_name));
std::wcout.imbue(std::locale());
#endif
}
int main(void)
{
init_locale();
for ( unsigned long i = 0; i < 0x10000UL; ++i )
if (iswprint(static_cast<wint_t>(i)))
std::wcout << static_cast<wchar_t>(i);
std::wcout << std::endl;
return EXIT_SUCCESS;
}
Upvotes: 2
Reputation: 416
When I run this code it runs fine:
int main(void)
{
std::cout << sizeof(char16_t) << "\n";
for (char16_t ch = u'\u0000'; ch <= u'\uffff'; ++ch)
std::wcout << " " << static_cast<char>(ch);
std::cout << "\n\n\n";
}
Upvotes: 0