Structure Initialization

Initialization: There are many methods to initialize the structure. Some of the following examples are:

Compile time initialization:

1.     

struct student

{

int roll_num;

float percentage;

} s1={1117,80.11};

In this example the members are initialized at the end of structure but before the semicolon which is used to terminate the structure.

2.     

struct student

{

int roll_num;

float percentage;

} ;

void main()

{

struct student s1={1117,80.11};

}

This is also very simple, only the difference is the structure variables are initialized in the body of the main() function.

Run time initialization: Run time initialization is done through the scanf() function.

3.      e.g:     

struct student

{

int roll_num;

char name[20]

};

void main()

{

struct student s1;

printf(“enter the values for student A:”)

scanf(“%d %s”,&s1.roll_num,s1.name);

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *