定义的字符串是否设置为所有零值?
我本可以发誓要读到一个定义但未初始化的字符串设置为所有零。换句话说,由
char string[10];
10个无效字符组成,
char string[10] = "Kevin";
由字母“ k”,“ e”,“ v”,“ i',',n”和5个nulls组成。
这是真的:
- 永远吗?
- 有时,取决于编译器?
- 绝不?
I could have sworn that I read that a string that was defined but not initialized was set to all NULLs. In other words, that
char string[10];
consisted of 10 null characters, and that
char string[10] = "Kevin";
consists of the letters 'K', 'e', 'v', 'i', 'n' and five nulls.
Is this true:
- Always?
- Sometimes, depending on the compiler?
- Never?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于,其中和变量。
是的。如果部分初始化了字符串或变量,则其余部分将填充
'\ 0'
或零。That depends where and on the variable.
Yes. If you partially initialize a string or a variable, the rest is filled with
'\0'
or zeros.如果没有初始化器(如您的第一个示例中),则数组的所有元素将具有不确定的值。来自
但是,如果您提供任何 的初始器(如第二个示例中),则数组中未明确集的任何元素都将初始化为零。从标准后期的几段中摘自:
(请注意,静态存储持续时间的
char
将初始化为零。)If there is no initializer present (as in your first example), then all elements of the array will have indeterminate values. From this C11 Draft Standard:
However, if you provide any sort of initializer (as in your second example), then any elements in the array that aren't explicitly set will be initialized to zero. From a few paragraphs later in the Standard:
(Note that a
char
of static storage duration will be initialized to zero.)