首頁 > 後端開發 > C++ > 主體

在C語言中,結構變數的存取方式如下所述

WBOY
發布: 2023-08-31 19:29:06
轉載
582 人瀏覽過

在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);
}
登入後複製

輸出

執行上述程式時,會產生以下結果-

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語言中,結構變數的存取方式如下所述的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!