+ 3

Why does arrays in c have none zero initial value to some elements?

https://code.sololearn.com/cjG5z7FdgXjx/?ref=app

15th Jan 2020, 11:14 PM
CASOY
CASOY - avatar
4 Respostas
+ 6
C was designed to be as close to assembler as possible while giving higher level language convenience. Therefore, everything is under your control. No point zeroing memory, if the code proceeds to put other values in it. But, as rodwynnejones pointed out it is simple to do so, if you want it.
15th Jan 2020, 11:34 PM
John Wells
John Wells - avatar
+ 3
when you create an array it holds base address.and it allocate memory in sequential order.when an array is not initialized it contains garbage value(unknown value).to avoid this type of problems we need to initialize array values to zero. for example; int arr[256]={0}; here all array values are initialized to zero anther example is, int arr[5]={1,2,3}; here we declare first 3 elements and the remaining elements automatically assigned to zero.
17th Jan 2020, 12:46 PM
Tejeshwar Reddy
Tejeshwar Reddy - avatar
+ 2
you need to do this:- int arr[256] = {0};
15th Jan 2020, 11:29 PM
rodwynnejones
rodwynnejones - avatar