This example tests the complete range of values for an unsigned char and the value EOF. If a locale bound to SBCS is loaded, then the output is the wide character representation for each input value. When a locale bound to DBCS is loaded, the input values that correspond to the lead byte of a double byte character do not have a wide character representation.
#include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <locale.h>
#define UPPER_LIMIT 0xFF
int main(void)
{
int wc;
int ch;
if (NULL == setlocale(LC_ALL, "")) {
printf("Locale could not be loaded\n");
exit(1);
}
for (ch = 0; ch <= UPPER_LIMIT; ++ch) {
wc = btowc(ch);
if (wc==WEOF) {
printf("%#04x is not a one-byte multibyte character\n", ch);
} else {
printf("%#04x has wide character representation: %#06x\n", ch, wc);
}
}
wc = btowc(EOF);
if (wc==WEOF) {
printf("The character is EOF.\n", ch);
} else {
printf("EOF has wide character representation: %#06x\n", wc);
}
return 0;
/***********************************************************************
If the locale is bound to SBCS, the output should be similar to:
0000 has wide character representation: 000000
0x01 has wide character representation: 0x0001
0x02 has wide character representation: 0x0002
...
0xfd has wide character representation: 0x00fd
0xfe has wide character representation: 0x00fe
0xff has wide character representation: 0x00ff
The character is EOF.
If the locale is bound to DBCS, the output should be similar to:
0000 has wide character representation: 000000
0x01 has wide character representation: 0x0001
0x02 has wide character representation: 0x0002
...
0x80 has wide character representation: 0x80
0x81 is not a one-byte multibyte character
0x82 is not a one-byte multibyte character
0x83 is not a one-byte multibyte character
...
0x9f is not a one-byte multibyte character
0xa0 has wide character representation: 0xa0
0xa1 has wide character representation: 0xa1
0xa2 has wide character representation: 0xa2
...
0xff has wide character representation: 0x00ff
The character is EOF.
***********************************************************************/
}