Click here to Skip to main content
15,898,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this case , does the output always produce zeros?

Output I have got:
0
0
0
0
0

What I have tried:

C#
#include<stdio.h>
int main()
{
    int i=0;
    int a[5]={};
    for(i=0;i<=4;i++)
    {
        printf("%d\n",a[i]);
    }
    return 0;
}
Posted
Updated 27-Apr-21 20:18pm
Comments
CPallini 28-Apr-21 2:12am    
It is already difficult to produce correct code using predictable features of the language, why do you insist on the unpredictables?
_-_-_-me 28-Apr-21 2:31am    
I am new to programming. I don't know anything. As I was studying the lecture class, it was given that when we initialize the number of elements less than the size of array, the remaining elements will be initialized to zero. So , I got the doubt that what would happen when no elements are given to initialize.

Thank you!!!
Richard MacCutchan 28-Apr-21 3:44am    
"I am new to programming. I don't know anything."
Then you should do what a number of people have already suggested and get a copy of the c programming language 3rd edition - Google Search[^], and actually learn it.
_-_-_-me 28-Apr-21 10:15am    
Thank you very much!
I downloaded the book "programming in c(3rd Edition) by stephen kochan ".
Is this the good one?
I will start learning it now .
Thank you very much!

Depends on the compiler, and on compiler options: some do always clear arrays when they are created, some don't - and in the later case it won't.

Technically, it's part of the C standard that this code:
C++
type name[size] = {};
will produce an initialized array - but not all compilers are 100% compliant.

If you are concerned about portability, you should probably explicitly use C library function - memset() - Tutorialspoint[^] to preset your array before you use it.
 
Share this answer
 
Comments
CPallini 28-Apr-21 2:17am    
5.However, technically it doesn't look 'part of the C standard' (compiled di -pedantic, as suggested on SO page :-D) :
foo.c: In function ‘main’:
foo.c:5:14: warning: ISO C forbids empty initializer braces [-Wpedantic]
    5 |     int a[5]={};
OriginalGriff 28-Apr-21 2:23am    
Oops! My mistake - I thought it was. But in my defence, it's been many decades since I read the C spec! :laugh:

Mind you, it's been decades since I last used C for anything as well ...
 
Share this answer
 

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