当前位置:网站首页>static 和 extern 关键字详解
static 和 extern 关键字详解
2022-06-12 13:26:00 【Li.CQ】
背景介绍
全局变量
- 外部变量:定义的变量能被本文件和其他文件访问(默认情况下,所有的全局变量都是外部变量,不同文件中的同名外部变量,都代表着同一个变量)
- 内部变量:定义的变量只能被本文件访问,不能被其他文件访问(不同文件中的同名内部变量,互不影响)
关键字介绍
- static
- 对变量的作用: 定义一个内部变量 ,使不同文件中的同名内部变量,互不影响; 通常用来声明一个只可以被本文件访问的变量;
- 对函数的作用: 定义和声明一个内部函数 通常用来声明一个只可以被本文件访问和调用的函数;
- extern
- 对变量的作用: 声明一个外部变量, 通常用来声明一个可以被其他文件访问的变量;
- 对函数的作用: 定义和声明一个外部函数(可以省略, 通常用来声明一个可以被其他文件调用的函数
static 用来修饰局部变量:
- 延长局部变量的生命周期:程序结束的时候,局部变量才会被销毁
- 并没有改变局部变量的作用域
使用方法
- 在函数中的用法
=============== aw_data.h ===============
#include <stdio.h>
// - 声明alloc_aw_data这个函数, 表示 alloc_aw_data 是一个外部函数;
extern aw_data * alloc_aw_data(int size);
=============== aw_data.c ===============
#include "aw_data.h"
// - 表示 alloc_aw_data 是一个外部函数; 只要调用这个函数, 那么就是调用这个函数;
aw_data * alloc_aw_data(int size){
// - 内部实现
}
// - 表示 write_bytes 是一个内部函数; 只有在这个文件内部调用这个函数时候, 才是write_bytes 的函数实现, 如果是其它文件调用这个函数, 并且其它文件有这个函数的定义, 那么就是另一个 write_bytes 的实现的调用了;
static void write_bytes(aw_data **awdata, const uint8_t *bytes, uint32_t count){
// - 内部实现
}
- 变量中的应用一
=============== Person.h ===============
#import <Foundation/Foundation.h>
// - 声明 kCellID 是一个外部变量, 此时任何一个包含了 Person.h的文件中 访问 kCellID的值都是 @"cell_ID";
extern NSString *const kCellID;
@interface Person : NSObject @end
=============== Person.m ===============
#import "Person.h"
// - 定义这个外部变量并为这个外部变量赋值
NSString *const kCellID= @"cell_ID";
@implementation Person @end
- 变量中的应用二
#import <Foundation/Foundation.h>
=============== Person.m ===============
#import "Person.h"
// - 定义这个外部变量并为这个外部变量赋值
int age = 10;
@implementation Person @end
#import <Foundation/Foundation.h>
=============== Animal.m ===============
#import "Animal.h"
@implementation Animal
- (void)eat {
// -此时这个 extern int age 声明一个 int 类型的外部变量 age, 此时所有访问这age的变量都是 访问的 Person.m 中定义的同一个age变量; 如果此时改为 extern int age1, 编译会报错, 所以这样写健壮性还是没有问题的.
extern int age;
NSLog(@"// - 李超群 console [log] ...%d", age);
}
@end
边栏推荐
- import torch_geometric 第一个图网络例子
- There was an error installing mysql. Follow the link below to CMD
- 入门深度学习与机器学习的经验和学习路径
- 创新实训(十二)项目总结
- Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
- 在 Debian 10 上独立安装MySQL数据库
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- Redis消息队列重复消费问题
- 关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
- Django note 21: querying databases using native SQL
猜你喜欢

Innovation training (x) advanced interface beautification

Volume mount and mirror creation

【刷题篇】超级洗衣机

Tensorrt, onnx to tensorrt in mmclas

torch_ geometric message passing network

成功定级腾讯T3-2,万字解析

leetcode 47. Permutations II full permutations II (medium)

Structure matérielle du système embarqué - introduction du Conseil de développement embarqué basé sur arm

Octopus network progress monthly report | may 1-May 31, 2022

Successful job hopping Ali, advanced learning
随机推荐
Experience and learning path of introductory deep learning and machine learning
Title: Yanghui triangle
Implementing pytorch style deep learning framework similartorch with numpy
1005: estimation of the earth's population carrying capacity
Multi source BFS problem template (with questions)
[you code, I fix] whitesource was officially renamed mend
Volume mount and mirror creation
Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
【刷题篇】超级洗衣机
关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
A brief introduction to Verilog mode
Software construction 03 regular expression
Structure matérielle du système embarqué - introduction du Conseil de développement embarqué basé sur arm
Redis message queue repeated consumption
hudi 键的生成(Key Generation)
RK3399平台开发系列讲解(内核调试篇)2.50、systrace的使用
torch_geometric message passing network
Embedded driver design
Application of binary search -- finding the square root sqrt of a number
JVM 运行时参数