1. 程式人生 > >第 16 章 C 預處理器和 C 庫(條件編譯)

第 16 章 C 預處理器和 C 庫(條件編譯)

struct FN getc con ade 定義 輸入 lap pla

技術分享圖片
 1 /*--------------------------------------
 2     names_st.h -- names_st 結構的頭文件
 3 --------------------------------------*/
 4 #ifndef NAMES_ST_H
 5 #define NAMES_ST_H
 6 
 7 #include <string.h>
 8 
 9 #define SLEN 32
10 
11 //結構聲明
12 struct names_st
13 {
14     char first[SLEN];
15     char last[SLEN];
16 }; 17 18 //類型定義 19 typedef struct names_st names; 20 21 //函數原型 22 void get_names(names*); 23 void show_names(const names*); 24 char* s_gets(char *st, int n); 25 26 #endif
names_st.h 技術分享圖片
 1 /*-----------------------------------------
 2     names_st.c -- 定義 names_st.h 中的函數
 3 -----------------------------------------
*/ 4 5 #include <stdio.h> 6 #include "names_st.h" //包含頭文件 7 8 //函數定義 9 void get_names(names *pn) 10 { 11 printf("Please enter your first name: "); 12 s_gets(pn->first, SLEN); 13 14 printf("Please enter your last name: "); 15 s_gets(pn->last, SLEN); 16 } 17 18 void show_names(const
names *pn) 19 { 20 printf("%s %s", pn->first, pn->last); 21 } 22 23 char* s_gets(char *st, int n) 24 { 25 char *ret_val, *find; 26 27 if (ret_val = fgets(st, n, stdin)) 28 { 29 if (find = strchr(st, \n)) 30 *find = \0; 31 else 32 while (fgetc(stdin) != \n) continue; //處理輸入行中的剩余字符 33 } 34 35 return ret_val; 36 }
names_st.c 技術分享圖片
 1 /*----------------------------------------
 2     useheader.c -- 使用 names_st 結構
 3 ----------------------------------------*/
 4 
 5 #include <stdio.h>
 6 #include "names_st.h"
 7 #include "names_st.h"    //第2次包含頭文件
 8 
 9 int main()
10 {
11     names candidate;
12 
13     get_names(&candidate);
14     printf("Let‘s welcome ");
15     show_names(&candidate);
16     printf(" to this program!\n");
17 
18     return 0;
19 }
useheader.c

技術分享圖片

第 16 章 C 預處理器和 C 庫(條件編譯)