Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print Box Characters in C (Windows)

How might one go about printing an em dash in C?

One of these: โ€”

Whenever I do: printf("โ€”") I just get a รน in the terminal.

Thank you.

EDIT: The following code is supposed to print out an Xs an Os looking grid with em dashes for the horizontal lines.

int main ()
{
    char grid[3][3] = {{'a', 'a', 'a'}, {'a', 'a', 'a'}, {'a', 'a', 'a'}};

    int i, j;

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            if (j != 0)
            {
                printf("|");
            }
            printf("  %c  ", grid[i][j]);
        }
        if (i != 2)
        {
            printf("\nโ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”\n");
        }
    }

    return 0;
}

Output: (The "รน"s should be "โ€”"s)

  a  |  a  |  a
รนรนรนรนรนรนรนรนรนรนรนรน
  a  |  a  |  a
รนรนรนรนรนรนรนรนรนรนรนรน
  a  |  a  |  a

EDIT: I'm on Windows 10 x64 using Codeblocks 16.01 with C11.

EDIT: I was informed of box characters and the question has morphed into how to print those, hence the title and tag change.

like image 805
Elliot Killick Avatar asked Dec 11 '25 17:12

Elliot Killick


1 Answers

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;
}
like image 154
Nominal Animal Avatar answered Dec 13 '25 07:12

Nominal Animal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!