解释 C 语言中结构变量的访问


结构是一种用户定义的数据类型,用于存储不同类型的数据集合。

结构与数组类似。唯一区别在于数组存储相同类型的数据,而结构存储不同类型的数据。

struct 关键字用于声明结构。

结构中的变量是结构的成员。

结构可以声明为以下形式 −

Struct structurename{
   //member declaration
};

示例

以下是 C 程序中访问结构变量的内容 −

 实时演示

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

输出

执行上述程序后,生成以下结果 −

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

更新于: 2021 年 03 月 08 日

1 千+ 浏览次数

开启你的 职业生涯

通过完成课程获得认证

开始
广告