当前位置:网站首页>Explanation of static and extern keywords
Explanation of static and extern keywords
2022-06-12 13:40:00 【Li. CQ】
Background introduction
Global variables
- External variables : The defined variables can be accessed by this file and other files ( By default , All global variables are external variables , External variables with the same name in different files , All represent the same variable )
- Internal variables : The defined variables can only be accessed by this file , Can't be accessed by other files ( Internal variables with the same name in different files , They don't influence each other )
Keyword Introduction
- static
- The effect on variables : Define an internal variable , Make internal variables with the same name in different files , They don't influence each other ; It is usually used to declare a variable that can only be accessed by this file ;
- Effect on function : Define and declare an internal function It is usually used to declare a function that can only be accessed and called by this file ;
- extern
- The effect on variables : Declare an external variable , It is usually used to declare a variable that can be accessed by other files ;
- Effect on function : Define and declare an external function ( It can be omitted , It is usually used to declare a function that can be called by other files
static Used to modify local variables :
- Extend the life cycle of local variables : At the end of the program , Local variables will be destroyed
- Does not change the scope of the local variable
Usage method
- Usage in functions
=============== aw_data.h ===============
#include <stdio.h>
// - Statement alloc_aw_data This function , Express alloc_aw_data Is an external function ;
extern aw_data * alloc_aw_data(int size);
=============== aw_data.c ===============
#include "aw_data.h"
// - Express alloc_aw_data Is an external function ; Just call this function , Then you call this function ;
aw_data * alloc_aw_data(int size){
// - Internal implementation
}
// - Express write_bytes It's an internal function ; Only when this function is called inside this file , It's just write_bytes Function implementation of , If other files call this function , And other files have the definition of this function , Then there is another one write_bytes The implementation of ;
static void write_bytes(aw_data **awdata, const uint8_t *bytes, uint32_t count){
// - Internal implementation
}
- Application of variables I
=============== Person.h ===============
#import <Foundation/Foundation.h>
// - Statement kCellID It's an external variable , At this point any one contains Person.h In the file of visit kCellID The values are all @"cell_ID";
extern NSString *const kCellID;
@interface Person : NSObject @end
=============== Person.m ===============
#import "Person.h"
// - Define the external variable and assign a value to it
NSString *const kCellID= @"cell_ID";
@implementation Person @end
- Application of variables II
#import <Foundation/Foundation.h>
=============== Person.m ===============
#import "Person.h"
// - Define the external variable and assign a value to it
int age = 10;
@implementation Person @end
#import <Foundation/Foundation.h>
=============== Animal.m ===============
#import "Animal.h"
@implementation Animal
- (void)eat {
// - At this point extern int age Make a statement int External variables of type age, At this point all access this age All variables are Access to the Person.m The same as defined in age Variable ; If it is changed to extern int age1, Error will be reported in compilation , So there's no problem with robustness .
extern int age;
NSLog(@"// - Li Chaoqun console [log] ...%d", age);
}
@end
边栏推荐
- FFmpeg 学习指南
- Informatics Olympiad all in one 2059: [example 3.11] buy a pen
- Implementing pytorch style deep learning framework similartorch with numpy
- Tensorrt, onnx to tensorrt in mmclas
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- Web3.0,「激发创造」的时代
- 1005: estimation of the earth's population carrying capacity
- Seeking magic square of order n with C language
- 【刷题篇】抽牌获胜的概率
- Codeforces 1634 F. Fibonacci additions - Fibonacci sequence addition, ideas
猜你喜欢

Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s

Innovation training (XI) summary of some bugs in the development process
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area

看完这一篇就够了,web中文开发

Realization of Joseph Ring with one-way ring linked list
![[brush title] probability of winning a draw](/img/f5/7e8dac944876f920db10508ec38e92.png)
[brush title] probability of winning a draw

Actual combat | realizing monocular camera ranging by skillfully using pose solution

Pytoch official fast r-cnn source code analysis (I) -- feature extraction

Scyther工具形式化分析Woo-Lam协议

单向环形链表实现约瑟夫环
随机推荐
Informatics Olympiad all in one 1000: introductory test questions
Codeforces 1629 B. GCD arrays - simple thinking
static 和 extern 关键字详解
The problem of Joseph in Informatics
1414: [17noip popularization group] score
D1 哪吒开发板 了解基本的启动加载流程
Return value of WaitForSingleObject
当字节跳动在美国输出中国式 996
Codeforces 1637 D. yet another minimization problem - Mathematics, DP
Getting started with NVIDIA Jetson nano Developer Kit
Pytoch official fast r-cnn source code analysis (I) -- feature extraction
Codeforces 1638 D. Big Brush —— BFS
创新实训(十一)开发过程中的一些bug汇总
AVFoundation
Codeforces 1637 C. Andrew and stones - simple thinking
Seekg, tellg related file operations
Semantic segmentation with pytorch
Implementing singleton mode of database under QT multithreading
Multi source BFS problem template (with questions)
Top 10 tips for visual studio code on Google