1. 程式人生 > >黑馬程式設計師——C語言基礎——結構體相關練習

黑馬程式設計師——C語言基礎——結構體相關練習

今天整理了C語言基礎學習過程中對結構體的一些知識,下面是程式碼片段:

#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]) {
    //定義一個結構體
    struct student {
        int num;            //學號
        char name[10];      //姓名
        int age;            //年齡
        char sex[2];        //性別
        float score;        //評分
    }studD, studE, studF;   //在宣告結構體的同時聲明瞭三個student型別的變數
    //定義student型別的結構體變數
    struct student studA;   //學生A
    struct student studB;   //學生B
    struct student studC;   //學生C
    studA.num = 1;
    strcpy(studA.name, "wangxinyu");
    studA.age = 21;
    strcpy(studA.sex, "men");
    studA.score = 89.57;
    printf("該學生序號:%d\n姓名:%s\n年齡:%d\n性別:%s\n評分:%.2f\n",
           studA.num, studA.name, studA.age, studA.sex, studA.score);
    return 0;
}

學習了C語言的結構體後,感覺和Java的VO比較相似,理解起來輕鬆了很多。

但是C語言是面向過程的語言,所以其中沒有類似物件的概念,故稱為結構體。