In standard C, you use wide characters and wide strings:
#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main(void)
{
setlocale(LC_ALL, "");
fwide(stdout, 1);
wprintf(L" \n");
wprintf(L" │ │ \n");
wprintf(L"───┼───┼───\n");
wprintf(L" │ │ \n");
wprintf(L"───┼───┼───\n");
wprintf(L" │ │ \n");
return EXIT_SUCCESS;
}
You can use wide character constants like L'┼'
; their conversion specifier for printf()
and wprintf()
functions is %lc
. Similarly, a wide string constant has an L
prefix, and its conversion specifier is %ls
.
Unfortunately, you are limited to the mangled version of C Microsoft provides, so it may or may not work for you.
The above code does not work in Windows, because Microsoft does not want it to. See Microsoft documentation on setlocale() for details:
The set of available locale names, languages, country/region codes, and code pages includes all those supported by the Windows NLS API except code pages that require more than two bytes per character, such as UTF-7 and UTF-8.
In other words, Microsoft's C localization is limited to one-byte code pages, and specifically excludes any Unicode locales. This is, however, purely part of Microsoft's EEE strategy to bind you, a budding developer, to Microsoft's own walled garden, so that you will not write actual portable C code (or, horror of horrors, avail yourself to POSIX C), but are mentally locked to the Microsoft model. You see, you can use _setmode() to enable Unicode output.
As I do not use Windows at all myself, I cannot verify whether the following Windows-specific workarounds actually work or not, but it is worth trying. (Do report your findings in a comment, Windows users, please, so I can fix/include this part of this answer.)
#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
static int set_wide_stream(FILE *stream)
{
return _setmode(_fileno(stream), _O_U16TEXT);
}
#else
static int set_wide_stream(FILE *stream)
{
return fwide(stream, 1);
}
#endif
int main(void)
{
setlocale(LC_ALL, "");
/* After this call, you must use wprintf(),
fwprintf(), fputws(), putwc(), fputwc()
-- i.e. only wide print/scan functions
with this stream.
You can print a narrow string using e.g.
wprintf(L"%s\n", "Hello, world!");
*/
set_wide_stream(stdout, 1);
/* These may not work in Windows, because
the code points are 0x1F785 .. 0x1F7AE
and Windows is probably limited to
Unicode 0x0000 .. 0xFFFF */
wprintf(L" \n");
/* These are from the Box Drawing Unicode block,
U+2500 ─, U+2502 │, and U+253C ┼,
and should work everywhere. */
wprintf(L" │ │ \n");
wprintf(L"───┼───┼───\n");
wprintf(L" │ │ \n");
wprintf(L"───┼───┼───\n");
wprintf(L" │ │ \n");
return EXIT_SUCCESS;
}