[习题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

  • 主要讨论作用域问题;

代码文件结构

[习题22]C语言关键词 extern_第1张图片
关键词 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.h  ex22.c  ex22.h  ex22_main  ex22_main.c  ex22.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 -DNDEBUG

ex22_main : ex22.o  ex22_main.c
    $(CC) $(CFLAGS) ex22_main.c ex22.o -o ex22_main

ex22.o : ex22.c
    $(CC) $(CFLAGS) -c -o ex22.o ex22.c

clean:
    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.h  ex22.c  ex22.h  ex22_main  ex22_main.c  ex22.o  Makefile  Makefile~

$ ./ex22_main

你可能感兴趣的:([习题22]C语言关键词 extern)