the structure is a user-defined data type, which is used to store a collection of different data types of data.
the structure is similar to an array. the only difference is that an array is used to store the same data types whereas, the structure is used to store different data types.
the keyword struct is for declaring the structure.
variables inside the structure are the members of the structure.
a structure can be declared as follows −
struct structurename{ //member declaration};
examplefollowing is the c program for accessing a structure variable −
live demo
struct book{ int pages; float price; char author[20];};accessing structure members in c#include<stdio.h>//declaring structure//struct{ char name[50]; int roll; float percentage; char grade[50];}s1,s2;void main(){ //reading user i/p// printf("enter name of 1st student : "); gets(s1.name); printf("enter roll number of 1st student : "); scanf("%d",&s1.roll); printf("enter the average of 1st student : "); scanf("%f",&s1.percentage); printf("enter grade status of 1st student : "); scanf("%s",s1.grade); //printing o/p// printf("the name of 1st student is : %s
",s1.name); printf("the roll number of 1st student is : %d
",s1.roll); printf("the average of 1st student is : %f
",s1.percentage); printf("the student 1 grade is : %s and percentage of %f
",s1.grade,s1.percentage);}
outputwhen the above program is executed, it produces the following result −
enter name of 1st student: bhanuenter roll number of 1st student: 2enter the average of 1st student: 68enter grade status of 1st student: athe name of 1st student is: bhanuthe roll number of 1st student is: 2the average of 1st student is: 68.000000the student 1 grade is: a and percentage of 68.000000
以上就是在c语言中,结构变量的访问方式如下所述的详细内容。