首页  >  文章  >  后端开发  >  在C语言中,结构变量的访问方式如下所述

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

WBOY
WBOY转载
2023-08-31 19:29:06574浏览

在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</p><p>",s1.name);
   printf("The roll number of 1st student is : %d</p><p>",s1.roll);
   printf("The average of 1st student is : %f</p><p>",s1.percentage);
   printf("The student 1 grade is : %s and percentage of %f</p><p>",s1.grade,s1.percentage);
}

输出

当上面的程序执行时,会产生以下结果−

rree

以上是在C语言中,结构变量的访问方式如下所述的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除