typedef struct 是什麼意思

typedef用於定義一種新類型
例如
定義了以下的結構
typedef struct student
{
int age;
int score;
}STUDENT;
那麼則有
STUDENT stu1;
就至關於struct student stu1;
上面的結構也能夠直接定義爲:
typedef struct
{
int age;
int score;
}STUDENT;
而後將STUDENT做爲新類型使用,好比STUDENT stu1;

typedef聲明新的類型來代替已有的類型的名字。
如:
typedef int INTEGER;
下面兩行等價
int i;
INTEGER i;
能夠聲明結構體類型:
typedef struct
{
int age;
int score;
}STUDENT;
定義變量:
只能寫成 STUDENT stu;
若是寫成
typedef struct student
{
int age;
int score;
}STUDENT;
下面三行等價:
STUDENT stu;
struct student stu;
student stu;
 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <conio.h>
 4 
 5 void main()
 6 {
 7     char letter;  // Letter typed by the user
 8 
 9     printf("Do you want to continue? (Y/N): ");
10 
11     letter = getch();          // Get the letter  
12     letter = toupper(letter);  // Convert letter to uppercase
13 
14     while ((letter != 'Y') && (letter != 'N'))
15     {
16         putch(3);                  // Beep the speaker
17         letter = getch();          // Get the letter  
18         letter = toupper(letter);  // Convert letter to uppercase
19     }
20 
21     printf("\nYour response was %c\n", letter);
22 }
相關文章
相關標籤/搜索