Initialization of One-Dimensional Arrays:
After
an array is declared, its elements must be initialized. Otherwise, they will
contain “garbage values”. An array can
be initialized at either of following stages:
- At compile time
- At run time
Compile Time Initialization:
We
can initialize the elements of arrays in the same way as the ordinary variables
when they are declared. The general form of initialization of arrays is:
The
values in the list are separated by commas. For example, the statement
int number [3] = { 0,0,0 };
will
declare the variable number as an array of size 3 and will assign zero to each
element. If the number of values in the list is less than the number of
elements, then only that many elements will be initialized. The remaining
elements will be set to zero automatically.
Run Time Initialization:
An
array can be explicitly initialized at run time. This approach is usually
applied for initializing large arrays. For example, consider the following
segment of C program.
int
i, a[5];
for(i=0;i<5;i++)
{
printf(“\n Enter Number :”);
scanf(“%d”,&a[i]);
}
Post a Comment