当前位置:网站首页>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
边栏推荐
- VGA显示彩条和图片(FPGA)
- 【刷题篇】抽牌获胜的概率
- 章鱼网络进展月报 | 2022.5.1-5.31
- import torch_geometric 加载一些常见数据集
- LeetCode滑动窗口刷题总结
- [you code, I fix] whitesource was officially renamed mend
- JVM runtime parameters
- 【VIM】. Vimrc configuration, vundle and youcompleteme have been installed
- Application of list and Dict
- Teach you how to create SSM project structure in idea
猜你喜欢

leetcode 47. Permutations II 全排列 II(中等)

移动应用出海的“新大陆”

实战 | 巧用位姿解算实现单目相机测距

import torch_geometric 的Data 查看

多源BFS问题 模板(附题)

Pytorch framework

torch_ geometric message passing network

嵌入式系統硬件構成-基於ARM的嵌入式開發板介紹

Innovation training (XI) summary of some bugs in the development process

Pre research of image scanning tool
随机推荐
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
How to balance multiple losses in deep learning?
leetcode 47. Permutations II 全排列 II(中等)
STM32F1与STM32CubeIDE编程实例-设备驱动-DHT11温度温度传感器驱动
Symbolic constant, const qualifier
torch_geometric message passing network
深度学习的多个 loss 是如何平衡的?
Innovation training (x) advanced interface beautification
[Title brushing] Super washing machine
[cloud native | kubernetes] in depth understanding of deployment (VIII)
2064: [example 2.1] exchange value
A brief introduction to Verilog mode
import torch_ Data view of geometric
【微信小程序开发】第1篇:开发工具安装及程序配置
Automatic Generation of Visual-Textual Presentation Layout
torch_ geometric message passing network
Design virtual network to realize communication between virtual machine instance and external network
Redis message queue repeated consumption
TCP的“非”可靠性
Getting started with NVIDIA Jetson nano Developer Kit