跳到主要內容

程式設計-C語言 / OLD / 2021-訊三甲( 蔡 ) / 0126- 結構資料型態

把結構當成 [ 資料型態] 的一種

1

 

#include <_stdio.h_>
#include <_stdlib.h_>
#include <_string.h_> 
struct student {        // 第4~8行宣告student結構
   int id;              //* 學生資料 */
   char name[20];
   int score;
};
 
int main() {
    struct student std1;      /* 宣告結構變數 */ 
struct student std2 = {2, "江小魚", 76}; /* 指定結構變數的值 */
    std1.id = 1;  
strcpy(std1.name, "陳小安");
    std1.score = 96;
/* 顯示學生資料 */
    printf("學號:%d\n", std1.id);  //第19~25行顯示結構內容
    printf("姓名:%s\n", std1.name);
    printf("成績:%d\n", std1.score);
    printf("--------------------\n"); 
    printf("學號:%d\n", std2.id);
    printf("姓名:%s\n", std2.name);
    printf("成績:%d\n", std2.score);
    
    return 0;
}

時間類別單位標題發佈點閱
跳至網頁頂部