C中是否允许空初始化,用于初始化数组元素? [英] Is empty initialization allowed in C, for initializing array elements ?

查看:166
本文介绍了C中是否允许空初始化,用于初始化数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
main()
{
int i,arr [ 5 ] = {};
for (i = 0 ; i< 5; i ++)
{
printf( %d \ n,arr [i]);
}
}



此代码产生以下输出:

0

0

0

0

0





问题:C中的数组元素是空的初始化,允许吗?



我尝试过:



我试图在一些在线IDE上运行上面的代码,我得到了相同的结果(如上所述)。



Ideone.com - NkJM0W - 在线C编译器&调试工具 [ ^ ]



我也试图在Stackoverflow上找到结果,但无法理解答案:

一个空的初始化列表是否有效的C代码? - 堆栈溢出 [ ^ ]

解决方案

据我所知,这是不允许的,但要小心,结果可能取决于编译器。



以下是该语言作者的参考书:

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https:// hassanolity。 files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]


给我一个空的初始化器似乎没有意义。在C和C ++中你可以这样写:



 int arr [5] = {0}; 


#include<stdio.h>
main()
{
    int i,arr[5]={};
    for(i=0;i<5;i++)
    {
        printf("%d\n",arr[i]);
    }
}


This code produces the following output :
0
0
0
0
0


QUESTION : Is empty initialization of array elements in C, allowed ?

What I have tried:

I tried to run the above code on some online IDEs and i got the same results (mentioned above).

Ideone.com - NkJM0W - Online C Compiler & Debugging Tool[^]

I also tried to find the result on Stackoverflow but couldn't understand the answer :
Is an empty initializer list valid C code? - Stack Overflow[^]

解决方案

As far as I know, it is not allowed, but be careful, the result may depend on compiler.

Here is the reference book by the authors of the language:
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


It seems rather pointless to have an empty initializer to me. In C and C++ you can write this :

int arr[5]={0};


这篇关于C中是否允许空初始化,用于初始化数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆