In C is there a pattern for variables in an array which are assigned 0 value? -
i want know if array of int elements declared in c. there pattern according values of array assigned 0 while others store garbage values? ex:
#include <stdio.h> void main() { int a[5]; int i; (i=0;i<=4;i++) { printf("%d\n",a[i]); } }
after compile , run program output,i.e.
0 0 4195344 0 2107770384
so zeroes there in a[0], a[1]
, a[3]
while a[2]
contains same value each time compiled , run whereas a[4]
value keeps on changing (including negative numbers). why happen fixed indices of array initialized 0 , have related past allocation of memory space?
this behaviour undefined , merely coincidence. when declare array on stack , not initialize array take on values (likely previous) stack frame.
aside: if wanted zero-fill array declared on stack (in constant time) can initialize using following initialization syntax:
int arr[ 5 ] = { 0 };
it write first element 0 , zero-fill rest of elements. however, if declared uninitialized array globally automatically zero-filled.
Comments
Post a Comment