[习题22]C语言关键词|[习题22]C语言关键词 extern
使用教材
【[习题22]C语言关键词|[习题22]C语言关键词 extern】《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794
- 完整源码 : learn-c-the-hard-way-lectures/ex22/
https://github.com/zedshaw/learn-c-the-hard-way-lectures/tree/master/ex22习题22
- 主要讨论作用域问题;
文章图片
关键词 extern.png
- extern关键词告诉编译器,变量是存在的, 但是它位于另外一个文件;
$ cc -Wall -g -DNDEBUG -c -o ex22.o ex22.c
$ cc -Wall -g -DNDEBUG ex22_main.c ex22.o -o ex22_main$ ls
dbg.hex22.cex22.hex22_mainex22_main.cex22.o
输出结果
anno@anno-m:~/Documents/mycode/ex22$ ./ex22_main
[INFO] (ex22_main.c:26) My name: Zed A.Shaw, age: 37
[INFO] (ex22_main.c:30) My age is now: 100
[INFO] (ex22_main.c:33) THE_SIZE is: 1000
[INFO] (ex22.c:31) I think size is : 1000
[INFO] (ex22_main.c:38) THE SIZE if now : 9
[INFO] (ex22.c:31) I think size is : 9
[INFO] (ex22_main.c:42) Ratio at first: 1.000000
[INFO] (ex22_main.c:43) Ratio again: 2.000000
[INFO] (ex22_main.c:44) Ratio once more: 10.000000
[INFO] (ex22_main.c:8) Count is: 4
[INFO] (ex22_main.c:16) count is at exit: 4
[INFO] (ex22_main.c:20) count after assign: 3000
[INFO] (ex22_main.c:8) Count is: 80
[INFO] (ex22_main.c:13) count in this scope is 100
[INFO] (ex22_main.c:16) count is at exit: 80
[INFO] (ex22_main.c:20) count after assign: 3000
[INFO] (ex22_main.c:51) count after calling scope_demo: 4
完整源码
ex22.h
#ifndef _ex22_h
#define _ex22_h// make THE_SIZE in ex22.c avaiable to other .c files
extern int THE_SIZE;
// gets and sets an internal static variable in ex22.c
int get_age();
void set_age(int age);
// updates a static variable that's inside update_ratio
double update_ratio(double ratio);
void print_size();
#endif
ex22.c
#include
#include "ex22.h"
#include "dbg.h"int THE_SIZE = 1000;
static int THE_AGE = 37;
int get_age()
{
return THE_AGE;
}void set_age(int age)
{
THE_AGE = age;
}double update_ratio(double new_ratio)
{
static double ratio = 1.0;
double old_ratio = ratio;
ratio = new_ratio;
return old_ratio;
}void print_size()
{
log_info("I think size is : %d", THE_SIZE);
}
ex22_main.c
#include "ex22.h"
#include "dbg.h"const char *MY_NAME ="Zed A.Shaw";
void scope_demo(int count)
{
log_info("Count is: %d", count);
if(count > 10) {
int count = 100;
// BAD! BUGS!log_info("count in this scope is %d", count);
}log_info("count is at exit: %d", count);
count = 3000;
log_info("count after assign: %d", count);
}int main(int agrc, char *argv[])
{
// test out THE_AGE accessors
log_info("My name: %s, age: %d", MY_NAME, get_age());
set_age(100);
log_info("My age is now: %d", get_age());
// test out THE_SIZE extern
log_info("THE_SIZE is: %d", THE_SIZE);
print_size();
THE_SIZE = 9;
log_info("THE SIZE if now : %d", THE_SIZE);
print_size();
// test the ration function staitc
log_info("Ratio at first: %f", update_ratio(2.0));
log_info("Ratio again: %f", update_ratio(10.0));
log_info("Ratio once more: %f", update_ratio(300.0));
// test the scope demo
int count = 4;
scope_demo(count);
scope_demo(count *20);
log_info("count after calling scope_demo: %d", count);
return 0;
}
[附加任务]编写
Makefile
Makefile
CC=cc
CFLAGS=-Wall -g -DNDEBUGex22_main : ex22.oex22_main.c
$(CC) $(CFLAGS) ex22_main.c ex22.o -o ex22_mainex22.o : ex22.c
$(CC) $(CFLAGS) -c -o ex22.o ex22.cclean:
rm ex22_main ex22.o
- 最新的文件应该是,可执行文件
ex22_main
-
ex22_main : ex22.o ex22_main.c
意味着:可执行文件ex22_main要生成,需要(即依赖)文件ex22.o
以及文件ex22_main.c
$ make ex22_main
gcc -Wall -g -DNDEBUG -c -o ex22.o ex22.c
gcc -Wall -g -DNDEBUG ex22_main.c ex22.o -o ex22_main$ ls
dbg.hex22.cex22.hex22_mainex22_main.cex22.oMakefileMakefile~$ ./ex22_main
推荐阅读
- 【生信技能树】R语言练习题|【生信技能树】R语言练习题 - 中级
- 一起来学习C语言的字符串转换函数
- C语言字符函数中的isalnum()和iscntrl()你都知道吗
- C语言浮点函数中的modf和fmod详解
- C语言中的时间函数clock()和time()你都了解吗
- C语言学习|第十一届蓝桥杯省赛 大学B组 C/C++ 第一场
- C语言解方程的根和判断是否是闰年
- C语言的版本比较
- 【C】题目|【C语言】题集 of ⑥
- echart|echart 双轴图开发