当前位置:网站首页>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 .
边栏推荐
- 函数(基本:参数,返回值)
- OWASP top 10 vulnerability Guide (2021)
- 指针函数(基础)
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- 电源管理总线 (PMBus)
- [phantom engine UE] realize the animation production of mapping tripod deployment
- Leetcode hot topic Hot 100 day 33: "subset"
- windows下Redis-cluster集群搭建
- [untitled]
- Stage experience
猜你喜欢
Realize the attention function of the article in the applet
Observable time series data downsampling practice in Prometheus
[crampon game] MC tutorial - first day of survival
level17
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
Label exchange experiment
Wenet: E2E speech recognition tool for industrial implementation
Interview related high-frequency algorithm test site 3
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
随机推荐
MySQL: view with subquery in the from clause limit
[finebi] the process of making custom maps using finebi
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
Here comes the Lantern Festival red envelope!
Fonction (sujette aux erreurs)
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
Observable time series data downsampling practice in Prometheus
Uncover the seven quirky brain circuits necessary for technology leaders
【虚幻引擎UE】实现测绘三脚架展开动画制作
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Scheduling system of kubernetes cluster
Practice | mobile end practice
Setting up redis cluster cluster under Windows
[phantom engine UE] the difference between running and starting, and the analysis of common problems
Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
学习MVVM笔记(一)
English topic assignment (26)
介绍汉明距离及计算示例