C Language Video

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484

Structure Initialization in C

How to initialize a structure variable?

Let us declare a student structure containing three fields i.e. name, roll and marks.

 

struct student

{

    char  name[100];

    int   roll;

    float marks;

};

 

Initialize structure using dot operator

In C, we initialize or access a structure variable either through dot . operator.

 

Example:

 

// Declare structure variable

struct student stu1;

 

// Initialize structure members

stu1.name  = "Manoj";

stu1.roll  = 12;

stu1.marks = 79.5;

 


 

Value initialized structure variable

C language also supports value initialization for structure variable. Means, you can initialize a structure to some default value during its variable declaration.

 

Example:

 

// Declare and initialize structure variable

struct student stu1 = { "Manoj", 12, 79.5 };

 

//Complete Program

 

 #include<stdio.h>

struct student

{

    char  name[100];

    int   roll;

    float marks;

};

 

int main()

{

    struct student stu1 = { "Manoj", 12, 79.5 };

   

    printf("Name of Student: %s\n", stu1.name);

    printf("Roll Number of Student: %d\n", stu1.roll);

    printf("Mark of Student: %f", stu1.marks);

}

C Language Video

Back - C Language Notes By Vivek Sir email: vivekdubey22@gmail.com (w) 9826424484