How to Print Box Characters in C (Windows)

2019-06-02 11:16发布

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.

1条回答
Anthone
2楼-- · 2019-06-02 12:09

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"                                                                    
查看更多
登录 后发表回答