当前位置:网站首页>C language - keyword 1
C language - keyword 1
2022-06-24 21:53:00 【Programming rookie】
auto
Scope of variable :
- The range that variables can act on . It's usually in curly brackets .
Life cycle of variable :
- The time it takes for a variable to be created and destroyed .
It can be considered that all local variables default to auto Of , But you don't have to write a compiler to know . This keyword is hardly used .
register
When you use register When decorating a variable , The compiler will place the variable in as far as possible CPU And not in memory , therefore CPU The speed of accessing this variable is very fast .
Some attention :
- 1,register The best decoration is local variables . Because global variables always occupy registers .
- 2, Will not be written . Writing is to write back to memory . Then the variable is set to register It doesn't make sense .
- It is better to read and write in high frequency .
- Can't be & Symbolic address .
- Don't be overused , Because registers are limited .
Why have header files
A lot of times we have this demand : Using functions or variables from another source file in one source file . Can it be used directly ?
- A function can , But it will warn , Function as long as the link is found .( It is limited to the functions written by yourself , To use library functions or other functions, you must include declarations ).
- Variables must be declared before they can be used , And it cannot be assigned at the time of declaration . Because the statement does not open up space .
But if I have 100 Source file , Each source file must use the same global variable , So what to do ? Should I write 100 Do you want to make a statement ?
therefore , A good solution is , Extract these declarations into 1 Shared files , To facilitate the maintenance of the project . This file is the header file . And every source file where you want to use variables or functions , Just include the header file .
- What the header file contains :

static
We know that global variables and functions can span files . Functions can be used without even declaration . Is there any way to ensure that your functions cannot be directly used by other source files ?
- static Modify global variable , Indicates that this variable can only be used inside the source file . Changed the scope of the variable . But it can be used indirectly .
- static Modify function , Indicates that the function can only be used inside the source file . But it can be used indirectly in a nested way .
- static The characteristics of for C Language provides the possibility of encapsulation . Ensure the safety of the project .
/************* test.c file ***************/
static int g_val = 100; //g_val Cannot be directly accessed by external files . because static
int show_val() {
printf("%d\n", g_val); // But it can go through show Function access ; because show Functions can be accessed by other source files
}
static void test(){
// By static modification , Cannot be accessed directly from outside
// do something...
}
void show_test(){
test(); // But you can nest , Indirect access to
}
- static Modify local variables , The life cycle of the variable
extern
- In front of the variable , Indicates that the variable is declared . Global variable declarations must carry extern, Otherwise, the compiler may be ambiguous . It is impossible to tell whether it is a definition or a declaration without it .
- Function can be without , Because the compiler can distinguish the definition and declaration of functions , But to keep good habits , It is suggested that you take it with you .

边栏推荐
- Network layer & IP
- Li Kou daily question - day 25 -496 Next larger element I
- About transform InverseTransformPoint, transform. InverseTransofrmDirection
- Volcano becomes spark default batch scheduler
- [theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
- Introduce the overall process of bootloader, PM, kernel and system startup
- Elegant custom ThreadPoolExecutor thread pool
- 数据链路层 && 一些其他的协议or技术
- Blender's simple skills - array, rotation, array and curve
- Unity about conversion between local and world coordinates
猜你喜欢

滤波数据分析

Data link layer & some other protocols or technologies

Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme

如何做到全彩户外LED显示屏节能环保

介绍BootLoader、PM、kernel和系统开机的总体流程

架构实战营 第 6 期 毕业设计

煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导

Network layer & IP

一文理解OpenStack网络

Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
随机推荐
Jianmu continuous integration platform v2.5.0 release
socket done
Slider controls the playback progress of animator animation
Interpretation of ebpf sockops code
SAP接口debug设置外部断点
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识
Multiplexer select
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
The most important thing at present
Implementing DNS requester with C language
即构「畅直播」上线!提供全链路升级的一站式直播服务
机器学习:梯度下降法
Multi task model of recommended model: esmm, MMOE
Elegant custom ThreadPoolExecutor thread pool
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Bld3 getting started UI
cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
Unity about conversion between local and world coordinates
openGauss内核:简单查询的执行