Click here to Skip to main content
14,697,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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[^]
Posted
Updated 4-Apr-17 12:38pm
v5

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};
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900