第 16 章 C 预处理器和 C 库(条件编译)

 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, '
'))
30             *find = '';
31         else
32             while (fgetc(stdin) != '
') 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!
");
17 
18     return 0;
19 }
useheader.c

原文地址:https://www.cnblogs.com/web1013/p/9229222.html