问题描述
在 C 语言中,我读到一半初始化的数组将用零填充其余元素(无论整数或字符数组).
In C, I have read that half-initialized arrays will be filled with zeros for the rest of the elements (irrespective of integer or char arrays).
例如:
int arr[10] = {3};
arr[4]
如果已初始化,则为 0,如果未初始化,则为垃圾值.
arr[4]
will be 0 if initialized and a junk value if not initialized.
我的问题是,以上内容是否适用于所有 C 编译器(或)根据编译器选项,可能会发生或不发生这种附加零?我在代码作曲家工作室(TI 的 IDE)工作.我想确保这适用于所有情况和所有编译器.
My question is, will the above work for all C compilers (or) this appending of zeros might occur or not occur depending on the compiler option? I am working in Code composer studio (TI's IDE). I want to ensure that this will work for all cases and all compilers.
推荐答案
行为由 C 标准指定.但是,如果您担心特定编译器的行为,为什么不写一个测试呢?
The behaviour is specified by the C Standard. But if you are worried about the behaviour of your specific compiler, why not write a test?
这篇关于C中数组的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!