printf C出错

时间:2018-02-28 08:08:46

标签: c printf

所以我在C中有这个代码,应该打印下面显示的地图。

printf ("(1)--------------------------------------------4T------5I        6P------7T-------------------------------------------(2)\n");
printf ("[%i]                                           [%i]     [%i]       [%i]     [%i]                                           [%i]\n", bs[1].leader, bs[4].leader, bs[5].leader, bs[6].leader, bs[7].leader, bs[2].leader);
printf (" \                                             |        \       /        |                                             / \n");
printf ("  \                                            |         \     /         |                                            /  \n");
printf ("   \                                           8T         \ 9T/         10T                                          /   \n");
printf ("    \                                         [ ]----------[ ]----------[ ]                                         /    \n");
printf ("     \                                         |            |            |                                         /     \n");
printf ("      \                                        |            |            |                                        /      \n");
printf ("      11P                                     12I          13I          14I                                     15P      \n");
printf ("      [ ]-------------------------------------[ ]----------[ ]----------[ ]-------------------------------------[ ]      \n");
printf ("        \                                      \            |            /                                      /        \n");
printf ("         \                                      16T        17I        18T                                      /         \n");
printf ("          \                                     [ ]--------[ ]--------[ ]                                     /          \n");
printf ("           \                                     \          |          /                                     /           \n");
printf ("           19M                                    20R      21P      22R                                    23M           \n");
printf ("           [ ]------------------------------------[ ]------[ ]------[ ]------------------------------------[ ]           \n");
printf ("             \                                      \               /                                      /             \n");
printf ("              24P                                    25I   26I   27I                                    28I              \n");
printf ("              [ ]                                    [ ]---[ ]---[ ]                                    [ ]              \n");
printf ("                \                                      \    |    /                                      /                \n");
printf ("                29I                                    30T 31P 32T                                    33I                \n");
printf ("                [ ]------------------------------------[ ]-[ ]-[ ]------------------------------------[ ]                \n");
printf ("                  \                                       \ | /                                       /                  \n");
printf ("                   ----------------------------------------(3)----------------------------------------                   \n");
printf ("                                                           [ ]                                                           \n");

问题是,它不打印。它会跳过很多字符。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

在C字符串中,您需要加倍\(即"\\")来代表一个'\'

所以你可以把你的代码写成:

void pprint(){

    int bs[6];
    bs[0] = 7;
    bs[1] = 1;
    bs[2] = 2;
    bs[3] = 3;
    bs[4] = 4;
    bs[5] = 5;
    bs[6] = 6;

    printf ("(1)--------------------------------------------4T------5I        6P------7T-------------------------------------------(2)\n");
    printf ("[%i]                                           [%i]     [%i]       [%i]     [%i]                                           [%i]\n", bs[1], bs[4], bs[5], bs[6], bs[0], bs[2]);
    printf (" \\                                             |        \\       /        |                                             / \n");
    printf ("  \\                                            |         \\     /         |                                            /  \n");
    printf ("   \\                                           8T         \\ 9T/         10T                                          /   \n");
    printf ("    \\                                         [ ]----------[ ]----------[ ]                                         /    \n");
    printf ("     \\                                         |            |            |                                         /     \n");
    printf ("      \\                                        |            |            |                                        /      \n");
    printf ("      11P                                     12I          13I          14I                                     15P      \n");
    printf ("      [ ]-------------------------------------[ ]----------[ ]----------[ ]-------------------------------------[ ]      \n");
    printf ("        \\                                      \\            |            /                                      /        \n");
    printf ("         \\                                      16T        17I        18T                                      /         \n");
    printf ("          \\                                     [ ]--------[ ]--------[ ]                                     /          \n");
    printf ("           \\                                     \\          |          /                                     /           \n");
    printf ("           19M                                    20R      21P      22R                                    23M           \n");
    printf ("           [ ]------------------------------------[ ]------[ ]------[ ]------------------------------------[ ]           \n");
    printf ("             \\                                      \\               /                                      /             \n");
    printf ("              24P                                    25I   26I   27I                                    28I              \n");
    printf ("              [ ]                                    [ ]---[ ]---[ ]                                    [ ]              \n");
    printf ("                \\                                      \\    |    /                                      /                \n");
    printf ("                29I                                    30T 31P 32T                                    33I                \n");
    printf ("                [ ]------------------------------------[ ]-[ ]-[ ]------------------------------------[ ]                \n");
    printf ("                  \\                                       \\ | /                                       /                  \n");
    printf ("                   ----------------------------------------(3)----------------------------------------                   \n");
    printf ("                                                           [ ]                                                           \n");
}

请注意,如果没有替换(使用%),您可能更喜欢puts()printf() - 但请注意puts()包含自己的最终换行符。< / p>