设为首页 加入收藏

TOP

第 16 章 C 预处理器和 C 库(条件编译)
2018-10-21 14:13:38 】 浏览:36
Tags:处理器 条件 编译
 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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数组的定义,初始化,赋值,传参.. 下一篇c/c++ 线性表之单向循环链表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目