当前位置:网站首页>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 .
边栏推荐
- Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
- [depth first search] Ji Suan Ke: Betsy's trip
- How to use C to copy files on UNIX- How can I copy a file on Unix using C?
- Executing two identical SQL statements in the same sqlsession will result in different total numbers
- How to generate rich text online
- Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
- 【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
- sql表名作为参数传递
- Grabbing and sorting out external articles -- status bar [4]
- Tensorflow customize the whole training process
猜你喜欢

【clickhouse】ClickHouse Practice in EOI

零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
![[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!](/img/aa/af98b588efd61d71b1b02609817c49.png)
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!

Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
![[solution] every time idea starts, it will build project](/img/fc/e68f3e459768abb559f787314c2124.jpg)
[solution] every time idea starts, it will build project

SPI communication protocol

Using SA token to solve websocket handshake authentication

Leetcode sum of two numbers

selenium 等待方式

MySQL index
随机推荐
【coppeliasim】6自由度路径规划
Computer graduation design PHP college classroom application management system
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
【无标题】数据库中一条查询SQL执行的过程
2 power view
Method of changing object properties
阿里测开面试题
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
Sword finger offer 38 Arrangement of strings
How to improve the level of pinduoduo store? Dianyingtong came to tell you
RDD partition rules of spark
Using SA token to solve websocket handshake authentication
Text editing VIM operation, file upload
PAT甲级 1033 To Fill or Not to Fill
Leetcode3, implémenter strstr ()
Gbase 8C database upgrade error
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
How to generate rich text online
1. Introduction to basic functions of power query
Campus second-hand transaction based on wechat applet