【发布时间】:2021-06-16 17:43:10
【问题描述】:
总体的一般问题是; C 中是否存在一些关于字符串初始化的奇怪之处,似乎没有人评论我可以找到参考/是否有一些特定的 Googlemancy 术语来研究我偶然发现的奇怪之处/是我还是有某种printf 和 strlen 如何处理 C 中的特定字符串的“错误”或“功能”?
我发现虽然 printf 应该打印一个字符串,但它只打印了它的一部分。我发现虽然 strlen 应该报告字符串的长度,但它只报告了其中的一部分。
我是否发现了某种错误,或者这是 C 的一个相当或非常晦涩的“特性”,在这种情况下,是这样一个“特性”的细节和讨论术语是什么?
考虑这段代码,以及随后的输出:
代码:
/* Header files for program support */
#include <stdio.h>
#include <string.h>
/* The primary program section */
int main(void)
{
/* Variable declaration and initialization*/
char aitch[30] = "Cat";
/* What gets done here */
printf("The string aitch is: %s.\n", aitch);
printf("strlen says aitch is %lu characters long.\n", strlen(aitch));
printf("aitch[10] is what is between >> and <<: >>%c<<.\n\n", aitch[10]);
printf("Now dish out a bunch of chopped liver.\n\n");
/* More variable declaration and initialization*/
aitch[11] = ' '; aitch[12] = 'C'; aitch[13] = 'h'; aitch[14] = 'o';
aitch[15] = 'p'; aitch[16] = 'p'; aitch[17] = 'e'; aitch[18] = 'd';
aitch[19] = ' '; aitch[20] = 'l'; aitch[21] = 'i'; aitch[22] = 'v';
aitch[23] = 'e'; aitch[24] = 'r';
/* Looking over the new stuff */
printf("aitch listed by character at this point is:\n\n");
printf("%c", aitch[0]); printf("%c", aitch[1]);
printf("%c", aitch[2]); printf("%c", aitch[3]);
printf("%c", aitch[4]); printf("%c", aitch[5]);
printf("%c", aitch[6]); printf("%c", aitch[7]);
printf("%c", aitch[8]); printf("%c", aitch[9]);
printf("%c", aitch[10]); printf("%c", aitch[11]);
printf("%c", aitch[12]); printf("%c", aitch[13]);
printf("%c", aitch[14]); printf("%c", aitch[15]);
printf("%c", aitch[16]); printf("%c", aitch[17]);
printf("%c", aitch[18]); printf("%c", aitch[19]);
printf("%c", aitch[20]); printf("%c", aitch[21]);
printf("%c", aitch[22]); printf("%c", aitch[23]);
printf("%c", aitch[24]); printf("\n\n");
/* Hang on, what was that last bit? */
printf("printf still says all of aitch is %s.\n", aitch);
printf("strlen still says all of aitch is %lu characters long.\n",
strlen(aitch));
printf("aitch[10] is still what is between >> and <<: >>%c<<.\n\n", aitch[10]);
printf("Now put something in the empty area.\n\n");
/* More variable declaration and initialization*/
aitch[3] = '_'; aitch[4] = '_'; aitch[5] = '_'; aitch[6] = '_';
aitch[7] = '_'; aitch[8] = '_'; aitch[9] = '_'; aitch[10] = '_';
/* Ok, _Now_ what??? */
printf("printf now says all of aitch is:\n\n%s.\n\n", aitch);
printf("strlen says all of aitch _now_ is %lu characters long.\n",
strlen(aitch));
printf("aitch[10] is now what is between >> and <<: >>%c<<.\n\n", aitch[10]);
}
输出:
% ./a.out
The string aitch is: Cat.
strlen says aitch is 3 characters long.
aitch[10] is what is between >> and <<: >><<.
Now dish out a bunch of chopped liver.
aitch listed by character at this point is:
Cat Chopped liver
printf still says all of aitch is Cat.
strlen still says all of aitch is 3 characters long.
aitch[10] is still what is between >> and <<: >><<.
Now put something in the empty area.
printf now says all of aitch is:
Cat________ Chopped liver.
strlen says all of aitch _now_ is 25 characters long.
aitch[10] is now what is between >> and <<: >>_<<.
【问题讨论】:
-
我会选择“晦涩的功能”。你知道什么是字符串,用C语言吗?
-
请告诉我们你对C中的字符串的理解。这显然是一个错误的理解。 C 字符串在第一个 NUL 字符处结束。您的“切碎的肝脏”代码写入 NUL 字符之后的字符,因此它当然不会更改原始字符串。
printf和strlen都没有错。 -
您应该提及您期望发生的事情。并重新审视字符串是什么。简而言之:如果它没有被
strlen计算或被printf("%s")打印,那么它不是该字符串的一部分。它可能是另一个字符串的一部分或根本没有字符串。 -
OT:25
printf("%c", aitch[x])可以替换为 for 循环,该循环的编写速度比复制/粘贴并将 x 替换为 0..25 更快 -
在第一部分,
aitch[3]仍然包含一个 nul 字符串终止符,因此strlen()报告相同的长度,printf()输出相同的字符串。strlen()和printf()都没有损坏。
标签: c string initialization printf strlen