typedef關鍵字

小王 -挺忙發表於2020-11-09

typedef關鍵字

typedef
typedef為C語言的關鍵字,跟if ,while。。等等一樣
這裡的資料型別包括內部資料型別(int,char等)和自定義的資料型別(struct等)。

和 struct 來匹配為了程式碼編寫簡潔
和普通的型別匹配,通過名字來獲取一些資訊。

給基本資料型別起別名;

typedef unsigned char u_int8;
typedef unsigned short int u_int16;
typedef unsigned int u_int32;
int main()
{  
    u_int8 data = 10;
	u_int16 data2 = 20;
    u_int32 data3 = 30;
 printf("%d,%d,%d\n",data,data2,data3); 
	system("pause");
	return 0;
}
   

執行結果;

在這裡插入圖片描述

給結構體型別起別名;

typedef struct Student
{
    int score;
    char *name;
}STU,*PSTU;
STU stu1 ;
    
    stu1.score = 100;
    
    printf("Score2 = %d\n",stu1.score);
    
    PSTU stu2;
    stu2 = (PSTU)malloc(sizeof(STU));
    stu2->score = 98;
    printf("score3 = %d\n",stu2->score);
    
    struct Student *stu3;
    stu3 = (struct Student*)malloc(sizeof(struct Student));
    stu3->score = 99;
    printf("score4 = %d\n",stu3->score);
    system("pause");
	return 0;
}

在這裡插入圖片描述

相關文章