定义的字符串是否设置为所有零值?

发布于 2025-01-21 21:16:04 字数 271 浏览 0 评论 0原文

我本可以发誓要读到一个定义但未初始化的字符串设置为所有零。换句话说,由

char string[10];

10个无效字符组成,

char string[10] = "Kevin";

由字母“ k”,“ e”,“ v”,“ i',',n”和5个nulls组成。

这是真的:

  1. 永远吗?
  2. 有时,取决于编译器?
  3. 绝不?

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:

  1. Always?
  2. Sometimes, depending on the compiler?
  3. Never?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2025-01-28 21:16:04

换句话说,那个char string [10];由10个无效字符组成

取决于,其中和变量。

char here_yes[10]; // 10 '\0' characters
int main() {
    char here_no[10]; // 10 unknown garbage values
    static char but_here_also_yes[10]; // also 10 '\0' characters
}

和char string [10] =“ kevin”;由字母“ k”,“ e”,“ v”,“ i”,'',n'组成。这是真的:总是吗?

是的。如果部分初始化了字符串或变量,则其余部分将填充'\ 0'或零。

char this_has_10_nulls[10] = "";
int main() {
    char this_has_ab_followed_by_8_nulls[10] = { 'a', 'b' };
    static char this_has_Kevin_followed_by_5_nulls[10] = "Kevin";
}

in other words, that char string[10]; consisted of 10 null characters,

That depends where and on the variable.

char here_yes[10]; // 10 '\0' characters
int main() {
    char here_no[10]; // 10 unknown garbage values
    static char but_here_also_yes[10]; // also 10 '\0' characters
}

and that char string[10] = "Kevin"; consists of the letters 'K', 'e', 'v', 'i', 'n' and five nulls. Is this true: Always?

Yes. If you partially initialize a string or a variable, the rest is filled with '\0' or zeros.

char this_has_10_nulls[10] = "";
int main() {
    char this_has_ab_followed_by_8_nulls[10] = { 'a', 'b' };
    static char this_has_Kevin_followed_by_5_nulls[10] = "Kevin";
}
沒落の蓅哖 2025-01-28 21:16:04

如果没有初始化器(如您的第一个示例中),则数组的所有元素将具有不确定的值。来自

6.7.9初始化


10如果具有自动存储持续时间的对象未明确初始化,则其值不确定。

但是,如果您提供任何 的初始器(如第二个示例中),则数组中未明确集的任何元素都将初始化为零。从标准后期的几段中摘自:

21如果在托架封闭列表中的初始化量少于
是汇总的元素或成员,或者是较少的字符
字符串字面用来初始化已知大小的数组,而不是那里
是阵列中的元素,剩余的汇总应为
与具有静态存储的对象的初始化相同
持续时间。

(请注意,静态存储持续时间的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:

6.7.9 Initialization


10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

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:

21 If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

(Note that a char of static storage duration will be initialized to zero.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文