Sunday 22 July 2012

Array Initialization with different storage classes

Array Initialization
Following are the examples that demonstrate this:
1) int n[4]={1,2,3,4};
2) int num[]={1,2,3,4};

If we do not specify the values of array elements they contain garbage values.

If we initialize the array where it is declared than it is optional to specify the dimension. As shown in second example above.

By default storage class of an array is auto. And if we do not initialize the array garbage value is taken.

If the storage class of an array is declared to be static, then all the array elements have default initial value as zero.

Let us understand the above by using an example:

/* Program for understanding the initialization of arrays*/

#include<stdio.h>
#include<conio.h>

void main()
{
int i;

//int x[];
//if we will uncomment the above declaration then it will cause an error
//because if we don't initialize the array at the time of declaration
//then we must specify its dimension

int y[]={1,2,3,4};
int marks[4];//Declaration of array
static int star[4];
printf("\n \nBy default value assigned to an array with storage class auto:");
for(i=0;i<4;i++) 
printf("\n marks[%d] is %d",i,marks[i]); //reading stored data from array 
printf("\n\n By default value assigned to an array with storage class static:");
for(i=0;i<4;i++)
{
printf("\n star[%d] is %d",i,star[i]); 
}
printf("\n\n Accessing elements of array(i.e. array y declared without specifying the dimension");
for(i=0;i<4;i++)
{
printf("\n y[%d] is %d",i,y[i]);

}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: