It is to be noted that only static or external variable can be initialized.
If incase, the structure of object or any element of struct are not initialized, the compiler will automatically assigned zero to the fields of that particular record.
employee data [3] = { { 146,’m’} ,{ 200, ‘f’ },{250 ,’m’}};
data [0].sal=0; data [1].sal=0; data [2].sal=0;
#include<stdio.h>
void main ()
{
int i;
struct student
{
long int rollno;
char sex;
float height;
float weight;
};
struct student data [3] = { {121,’m’,5.7,59.8},{122,’f’,6.0,65.2},{123,’m’, 6.0, 7.5} };
clrscr ();
printf (“the initialized contents are:\n”);
for ( i=0; i< =2; i++)
{
printf (“%d/n ** Record is \n “, data [i].rollno);
printf (“%c\n“, data [i] .sex);
printf (“%f\n”, data [i].height);
printf (“%f\n”, data [i]. weight);
}
}
Output:
The initialized contents are:
121
** Record is
m
5.700000
59.799999
122
** Record is
f
6.00000
65.19997
123
** Record is
m
6.000000
7.500000