英文:
In C printf print on stdout "\0" but shouldn't
问题
我使用以下命令来在标准输出上打印,即使是由`printf(""someString"")`打印的隐藏字符,比如`"\t"`或`"\n"`,问题是即使`"我使用以下命令来在标准输出上打印,即使是由`printf(""someString"")`打印的隐藏字符,比如`"\t"`或`"\n"`,问题是即使`"\0"`也被打印出来,但不应该。
输出:
0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
我尝试以以下方式打印:
printf(""%d"",mov);
printf(""\t"");
printf(""%d"",wets);
printf(""\n"");
for (i = mov; i >= 0; i--) {
printf(""%c"",movement[i]);
}
"`也被打印出来,但不应该。
输出:
0000000 3 1 \t 2 7 \n 我使用以下命令来在标准输出上打印,即使是由`printf(""someString"")`打印的隐藏字符,比如`"\t"`或`"\n"`,问题是即使`"\0"`也被打印出来,但不应该。
输出:
0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
我尝试以以下方式打印:
printf(""%d"",mov);
printf(""\t"");
printf(""%d"",wets);
printf(""\n"");
for (i = mov; i >= 0; i--) {
printf(""%c"",movement[i]);
}
S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E 我使用以下命令来在标准输出上打印,即使是由`printf(""someString"")`打印的隐藏字符,比如`"\t"`或`"\n"`,问题是即使`"\0"`也被打印出来,但不应该。
输出:
0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
我尝试以以下方式打印:
printf(""%d"",mov);
printf(""\t"");
printf(""%d"",wets);
printf(""\n"");
for (i = mov; i >= 0; i--) {
printf(""%c"",movement[i]);
}
我使用以下命令来在标准输出上打印,即使是由`printf(""someString"")`打印的隐藏字符,比如`"\t"`或`"\n"`,问题是即使`"\0"`也被打印出来,但不应该。
输出:
0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
我尝试以以下方式打印:
printf(""%d"",mov);
printf(""\t"");
printf(""%d"",wets);
printf(""\n"");
for (i = mov; i >= 0; i--) {
printf(""%c"",movement[i]);
}
0000046
我尝试以以下方式打印:
printf(""%d"",mov);
printf(""\t"");
printf(""%d"",wets);
printf(""\n"");
for (i = mov; i >= 0; i--) {
printf(""%c"",movement[i]);
}
注意:我已经保留了原文中的HTML实体,如"
,以保持代码部分的完整性。
英文:
I use
./file.exe test.in | od -c
for print on the stdout even the hidden char printed by a printf("someString")
, like "\t"
or "\n"
, the problem is that even "\0"
gets printed, but shouldn't.
OUTPUT:
0000000 3 1 \t 2 7 \n 0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E 0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
0000000 3 1 \t 2 7 \n \0 S S S S E E E E E
0000020 N N W W N N E E E E E S S S S S
0000040 S S S E \0 \0
0000046
0000046
i tried print i this way:
printf("%d",mov);
printf("\t");
printf("%d",wets);
printf("\n");
for (i = mov; i >= 0; i--) {
printf("%c",movement[i]);
}`
in line 1-4 i tried to use a printf()
for each char that need to be printed, instead using a just one printf
答案1
得分: 1
你问题的根本似乎是对for
循环的不寻常使用。在可能的情况下,应避免所有没有惯用形式的循环:
for(size_t i=0; i<n; i++)
其他类型的循环对每个人,包括你自己来说,都难以阅读。非惯用循环从不同的索引开始,或以不同于<n
的条件结束,或使用其他迭代修改而不是简单的++
,或者以倒数方式计数等。有时候确实没有其他办法,那么使用其他形式是可以的,但不要不必要地这样做,因为代码会变得更难阅读。
在你的情况下,你从计数器开始,然后向下计数,同时i>=0
。这在逻辑上相当于从i=0;
向i<=n
递增,意味着多一个项目。在大多数情况下,这意味着数组越界访问错误。
要以相反的顺序显示内容,你可以像这样编写循环:
for(size_t i=0; i<mov; i++)
{
printf("%c", movement[mov-i-1]); // -1因为数组是从0开始索引的
}
英文:
The root of your problems seems to be exotic use of for
loops. Whenever possible, avoid all loops which doesn't have the idiomatic form:
for(size_t i=0; i<n; i++)
Other kind of loops are needlessly hard to read for everyone including yourself. Non-idiomatic loops start at a different index than 0 or end at a different condition than <n
, or they use other iterator modifications than a simple ++
, or they count downwards etc. Sometimes there is no other way around it and then it's OK to use other forms, but don't do it needlessly because the code turns harder to read.
In your case you start at a counter and count downwards while i>=0
. That is the logical equivalent of counting upwards from i=0;
to i<=n
, meaning one additional item. In most cases meaning an array out of bounds access bug.
To display something in the reverse order, you could write the loop like this instead:
for(size_t i=0; i<mov; i++)
{
printf("%c", movement[mov-i-1]); // -1 since arrays are 0-indexed
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论