結構體是使用者定義的資料類型,用於儲存不同資料類型的資料的集合。
結構體類似於陣列。唯一的區別是數組用於存儲相同的資料類型,而結構體用於存儲不同的資料類型。
關鍵字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中文網其他相關文章!