当前位置:网站首页>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 .

边栏推荐
- #国企央企结构化面试#国企就业#墨斗互动就业服务管家
- 使用region折叠代码
- 【吴恩达笔记】卷积神经网络
- Li Kou daily question - day 26 -496 Next larger element I
- Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
- Data link layer & some other protocols or technologies
- socket done
- Interpretation of ebpf sockops code
- Jianmu continuous integration platform v2.5.0 release
- 建木持续集成平台v2.5.0发布
猜你喜欢

BPF_ PROG_ TYPE_ SOCKET_ Filter function implementation

Multi view function in blender

我国SaaS产业的发展趋势与路径

VirtualBox virtual machine installation win10 Enterprise Edition

Docking of arkit and character creator animation curves

LeetCode-513. Find the value in the lower left corner of the tree

Network layer & IP

多路转接select

降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)

即构「畅直播」上线!提供全链路升级的一站式直播服务
随机推荐
Antdb database online training has started! More flexible, professional and rich
Wireshark packet capturing skills summarized by myself
Li Kou daily question - day 25 -496 Next larger element I
Visit Amazon memorydb and build your own redis memory database
Vscode netless environment rapid migration development environment (VIP collection version)
Shengzhe technology AI intelligent drowning prevention service launched
suspense组件和异步组件
心楼:华为运动健康的七年筑造之旅
Guava中这些Map的骚操作,让我的代码量减少了50%
最大流问题
旅行商问题(TSP)的相关论文总结
leetcode_1470_2021.10.12
Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
双链表实现
建木持续集成平台v2.5.0发布
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
架构实战营 第 6 期 毕业总结
Jianmu continuous integration platform v2.5.0 release
Li Kou daily question - day 26 -496 Next larger element I
Kubernetes 集群中流量暴露的几种方案