当前位置:网站首页>Keyword static
Keyword static
2022-07-06 02:16:00 【Awei II】
Common keywords
auto break case char const continue default do double else enum extern float for goto if int register return short signed sizeof static struct switch typedef union unsigned void volatile while
among static Keywords are more important , Let's talk about it alone
static Keywords have three functions 1. Modify local variables 2、 Modify global variable 3、 Modify function
1、 Modify local variables
First of all, look at such a string of code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void test()
{
int a = 1;
a++;
printf("%d\2", a);
}
int main()
{
int i = 0;
while (i < 10)
{
test();
i++;
}
return 0;
}
The output of the above code is as follows

Local variables in the above code a Didn't add static, So every time you jump out of the loop ,a The value of will become a++ The value of is 2, So every time the output value is 2
Now look at local variables a And before static, The following code snippet
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void test()
{
static int a = 1;
a++;
printf("%d\2", a);
}
int main()
{
int i = 0;
while (i < 10)
{
test();
i++;
}
return 0;
}
The output of the above code segment is as follows

Join in static keyword , Every time I output a The values of are preserved , So it will be based on the original ++
2、 Modify global variable
First, define a global variable in a source file

Then output the global variable under another source file

You can see the global variables g_val It can output normally .
Now? , If you add static

Then continue to output this global variable under another source file

Can see , Something went wrong , use static The defined global variables cannot be output under another source file , This is because static Defined global variables can only be in their own internal .c In the source file , But it cannot be used in the whole project , But generally defined global variables can be used in the whole project . Global variables , Other files can be used because global variables are externally linked . however static After decorating the global variables , It has the internal connection property , So it can't be used in the whole project .
3、static Decorate global functions
First, define a global function under a source file , as follows

Call this function under another source file , The following is after the call

Can see , Under normal circumstances, the global functions defined can be called under other source files
Now when defining the global function, add static

Then call under other source files , give the result as follows

Call error , This is because of the relationship with static Modifying global variables is similar to , Add static It means to change the external connectivity of the original global function into internal connectivity , As a result, it cannot be used in the whole project .
边栏推荐
- 2022 PMP project management examination agile knowledge points (8)
- Selenium element positioning (2)
- Compact lidar global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
- The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup
- 0211 embedded C language learning
- Bidding promotion process
- How to set an alias inside a bash shell script so that is it visible from the outside?
- Redis如何实现多可用区?
- Redis key operation
- 729. My schedule I / offer II 106 Bipartite graph
猜你喜欢

SPI communication protocol

MySQL index

在线怎么生成富文本

Multi function event recorder of the 5th National Games of the Blue Bridge Cup

02. Go language development environment configuration

2022 PMP project management examination agile knowledge points (8)

How does redis implement multiple zones?

Use image components to slide through photo albums and mobile phone photo album pages
![[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)](/img/ec/7fcdcbd9c92924e765d420f7c71836.jpg)
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)

PAT甲级 1033 To Fill or Not to Fill
随机推荐
SSM assembly
leetcode-两数之和
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
抓包整理外篇——————状态栏[ 四]
Leetcode3. Implement strstr()
[solution] every time idea starts, it will build project
Redis如何实现多可用区?
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
【coppeliasim】高效传送带
Computer graduation design PHP enterprise staff training management system
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
Text editing VIM operation, file upload
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
leetcode-2. Palindrome judgment
MySQL learning notes - subquery exercise
RDD conversion operator of spark
阿裏測開面試題
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3