extern关键字的使用
来源:互联网 发布:国航网上值机软件 编辑:程序博客网 时间:2024/06/11 18:25
extern关键字的使用
用extern 声明外部变量
在同一个文件中声明的外部变量
作用域:从用extern声明之后,到文件结束。
例子:
#include<stdio.h>int add(int, int);int main(){ int result; extern int a; extern int b; result = add(a, b); printf("a add b is %d\n",result); return 0;}int a = 30;int b = 20;int add(int x, int y){ return a + b;}
在多个文件中声明外部变量
作用域:多个文件组成的工程中,一个文件引用另一个文件以定义的外部变量时,只需要在使用该变量前用extern声明。
例子:
//a.c#include<stdio.h>int TIMES = 5;extern int add(int a, int b);int main(){ int a = 10; printf("%d*%d = %d\n",a, TIMES, times(a)); return 0;}
//b.c#include<stdio.h>extern int TIMES;int times(int x){ return x * TIMES;}
利用gcc 工具编译gcc a.c b.c -o times, 再运行 ./times, 结果为 10 * 5 = 50 . 其中,在a.c 文件中定义 TIMES = 5, 在 b.c 中引用 TIMES 时,需要用 extern 关键字声明其为外部变量,否则编译会找不到该变量.
在多个文件中声明外部结构体变量
前一节中,只是适合一般变量的外部声明,但是对于声明外部结构体变量时,则有些不同,需要加以注意。
例子:
//a.c#include<stdio.h>#include "b.h"#include "c.h"A_class local_post = {1, 2, 3};A_class next_post = {10, 9, 8};int main(int argc, char *argv[]){ A_class ret; print("fist point", local_post); print("second point",next_post); ret = fun(local_post, next_post); printf("the vector is (%d %d %d)\n",ret.x, ret.y, ret.z); return 0;}
//b.c#include <stdio.h>#include "b.h"A_class fun(A_class first, A_class next){ A_class ret; ret.x = next.x - first.x; ret.y = next.y - first.y; ret.z = next.z - first.z; return ret;}
//c.c#include <stdio.h>#include "b.h"int print(char *str, A_class post){ printf("%s:(%d,%d,%d)\n",str,post.x,post.y,post.z); return 0;}
//b.h#ifndef __B_H#define __B_H#if 1typedef struct { int x; int y; int z;} A_class;#endifextern A_class local_post;extern A_class fun(A_class x, A_class y);#endif
//c.h#ifndef __C_H#define __C_Hextern int print(char *, A_class post);#endif
利用 gcc 工具编译 gcc a.c b.c c.c -o demo ,在运行 ./demo ,结果为:
fist point:(1,2,3)second point:(10,9,8)the vector is (9 7 5)
小结: 在 a.c 文件中定义全局变量 A_class local_post 结构体,并且调用 b.c 中的接口函数 A_class fun(int x, int y) 和 c.c 中的int print(char *str, A_class post), 在b.h 文件中,如果屏蔽掉 A_class 的实现,而在 b.h以外实现,此生将编译出错,由于 c.c 文件中用到 A_class 定义的类型,所以需要在该文件中包含 b.h。
这里需要说明的是,如果调用外部结构体等多层结构体变量时,需要对这种变量进行实现,使用时,加上模块的头文件即可,否则会报错。
实际工程中,模块化程序文件,一般提供一个 .c 和一个 .h 文件, 其中 .h 文件被 .c 文件调用, .h 文件中实现.
用 extern 声明外部函数
- 定义函数时,在函数的返回值类型前面加上extern 关键字,表示此函数时外部函数,可供其他文件调用,如 extern int func(int x, int y), C语言规定,此时 extern 可以省略,隐形为外部函数。
- 调用此函数时,需要用extern 对函数做出声明.
作用域: 使用 extern 声明能够在一个文件中调用其他文件的函数,即把被调用函数的作用域扩展到本文件。C语言中规定,声明时可以省略 extern。
例子:
//a.c#include <stdio.h>#include "b.h"int main(int argc, char *argv[]){ int x = 10, y = 5; printf("x = 10, y = 5\n"); printf("x + y = %d\n", add(x, y)); printf("x - y = %d\n", sub(x, y)); printf("x * y = %d\n", mult(x, y)); printf("x / y = %d\n", div(x, y)); return 0;}
//b.h#ifndef __F_H#define __F_Hextern int add(int, int);extern int sub(int, int);extern int mult(int, int);extern int div(int, int);#endif
//b.c#include <stdio.h>int add(int x, int y){ return (x + y);}int sub(int x, int y){ return x - y;}int mult(int x, int y){ return x * y;}int div(int x, int y){ if (y != 0) { return (x / y); } printf("mult()fail second para can not be zero!\n"); return -1;}
利用 gcc 工具编译 gcc a.c b.c -o demo ,再运行 ./demo , 结果为:
x = 10, y = 5x + y = 15x - y = 5x * y = 50x / y = 2
小结:由上面简单的例子可以看出,在 b.h 文件中声明好 b.c 的函数,使用时,只需要在 a.c 中包含 #include “b.h” 头文件即可,这样就可以使用 b.c 的接口函数了,在实际工程中,通常也是采用这种方式, .c 文件中实现函数,.h文件中声明函数接口,需要调用.c文件的函数接口时,只需包含.h文件即可。
参考文章
- extern关键字的使用
- extern关键字的使用
- 关于extern关键字的使用
- C语言中extern关键字的使用
- C++中extern关键字的使用
- 关键字 extern 使用
- extern 关键字的作用
- extern关键字的作用
- extern关键字的作用
- extern关键字的作用
- extern关键字的测试
- 【extern】如何使用关键字extern "C"?
- extern 关键字的使用 《程序员的自我修养》·笔记
- C语言的static和extern关键字的使用
- C++中extern关键字使用
- 为什么要使用关键字 extern
- C++中extern关键字使用
- C++中extern关键字使用
- IntelliJ IDEA优秀插件(编程通用)
- 学习kubernetes
- js获取当前时间 显示的格式类型2017-06-03 16:13:12
- erase方法+erase(lob,amount,offset)+使用erase方法
- Caffe 深度学习框架上手教程
- extern关键字的使用
- Let the Balloon Rise
- ZOJ 2588 Burning Bridges
- 倍增LCA模板
- ##报错:DrawerLayout must be measured with MeasureSpec.EXACTLY.
- ssm整合:
- LiteIDE中漂亮的几款QSS
- mybatis快速入门
- 中间缓存变量机制 i++