当前位置:网站首页>Variable category (automatic, static, register, external)
Variable category (automatic, static, register, external)
2022-07-05 04:34:00 【On the bald Road】
1.auto Automatic variable
auto int a = 0; Will report a mistake !!!
An error message appears :"auto” Cannot be combined with any other type specifier , Because the new version C++ Definition auto Cannot be combined with any type .
!!! If a variable is declared without keywords , That's automatic .
2,static Static variables .
Hopefully, after the function call ends , Among them, the variable value will not disappear, and you can use static variables . The next time you call a function , This variable calls the value at the end of the last function call .
for example
#include<iostream>
using namespace std;
int fun(int);
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
cout << i << "!=" << fun(i) << endl;
}
return 0;
}
int fun(int i) {
/*auto int a = 0; Will report a mistake !!!*/
static int n = 1;
n = i * n;
return n;
}The running result is :
1!=1
2!=2
3!=6
4!=24
5!=120
Some properties of static variables : Assign initial value to , The default is 0; and auto The default is random , And an error will be reported .
register Register variables
Some variables used many times , In order to improve the efficiency of execution ,c++ It is allowed to store the value of a local variable in CPU In the register of , No need to call from memory .
extern Declared external variables
Its scope starts from the definition of variables , To the end of this procedure , In this scope , Global variables can be referenced by various functions in this document .!!!! At compile time, global variables will be allocated in static storage .
matters needing attention : A program that contains multiple files , If you define the same variable in different files , Errors will be reported when linking , So put a variable extern To declare as an external variable .
--------------------------------------------------------------------------------------------------------------------------------
Add :
1. Automatic variable Register variables For dynamic storage ; Static local variables , Static external variables , External variables are stored statically .
2. Internal function for example :static int func(int a) Limit functions to this document , If there are internal functions with the same name in different files , Mutual interference . Referred to as localization ;
External function example :extern int func(int a) Expressed as an external function that can be called by other files .
边栏推荐
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- [crampon game] MC tutorial - first day of survival
- 直播预告 | 容器服务 ACK 弹性预测最佳实践
- 3 minutes learn to create Google account and email detailed tutorial!
- 网络安全-记录web漏洞修复
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
- [crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
- 包 类 包的作用域
- Neural networks and deep learning Chapter 3: linear model reading questions
- Function (basic: parameter, return value)
猜你喜欢

User behavior collection platform

Raki's notes on reading paper: soft gazetteers for low resource named entity recognition

2022-2028 global and Chinese video coding and transcoding Market Research Report
![[crampon game] MC tutorial - first day of survival](/img/81/82034c0382f545c39bd8c15f132ec7.jpg)
[crampon game] MC tutorial - first day of survival
![Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/bf/fb4e85143d1461a2026c88cda4a18d.jpg)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar

官宣!第三届云原生编程挑战赛正式启动!

C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)

MacBook安装postgreSQL+postgis
![[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices](/img/45/380e739f5eed33626c363756f814d3.png)
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices

About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
随机推荐
Raki's notes on reading paper: code and named entity recognition in stackoverflow
蛇形矩阵
English topic assignment (26)
OWASP top 10 vulnerability Guide (2021)
Observable time series data downsampling practice in Prometheus
Study notes 7
电源管理总线 (PMBus)
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Learning MVVM notes (1)
Mixed compilation of C and CC
Here comes the Lantern Festival red envelope!
Common features of ES6
直播预告 | 容器服务 ACK 弹性预测最佳实践
Managed service network: application architecture evolution in the cloud native Era
All in one 1413: determine base
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
Realize the attention function of the article in the applet
Ffmepg usage guide
2022-2028 global and Chinese FPGA prototype system Market Research Report
直播預告 | 容器服務 ACK 彈性預測最佳實踐