gpt4 book ai didi

c - printf C 错误

转载 作者:行者123 更新时间:2023-11-30 18:23:51 25 4
gpt4 key购买 nike

所以我用 C 语言编写了这段代码,它应该打印如下所示的 map 。

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");

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

最佳答案

在 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() 包含自己的最终换行符。

关于c - printf C 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49024728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com