在C语言中,结构变量的访问方式如下所述

2023年 8月 31日 48.9k 0

在C语言中,结构变量的访问方式如下所述

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
};

登录后复制

Example

Following 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
//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);
}

登录后复制

Output

When the above program is executed, it produces the following result −

enter Name of 1st student: Bhanu
enter Roll number of 1st student: 2
Enter the average of 1st student: 68
Enter grade status of 1st student: A
The name of 1st student is: Bhanu
The roll number of 1st student is: 2
The average of 1st student is: 68.000000
The student 1 grade is: A and percentage of 68.000000

登录后复制

以上就是在C语言中,结构变量的访问方式如下所述的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论