当前位置:网站首页>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 .
边栏推荐
- Ffmepg usage guide
- web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
- Mxnet imports various libcudarts * so、 libcuda*. So not found
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- WeNet:面向工业落地的E2E语音识别工具
- [uniapp] system hot update implementation ideas
- Observable time series data downsampling practice in Prometheus
- 函数(基本:参数,返回值)
- [untitled]
- MacBook安装postgreSQL+postgis
猜你喜欢
Observable time series data downsampling practice in Prometheus
揭秘技术 Leader 必备的七大清奇脑回路
Cookie learning diary 1
Common features of ES6
Label exchange experiment
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Managed service network: application architecture evolution in the cloud native Era
概率论与数理统计考试重点复习路线
How to get the first few pieces of data of each group gracefully
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
随机推荐
Mixed compilation of C and CC
MySQL: view with subquery in the from clause limit
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
How can CIOs use business analysis to build business value?
[untitled]
Neural network and deep learning Chapter 1: introduction reading questions
The remainder operation is a hash function
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
Kwai, Tiktok, video number, battle content payment
level18
PR video clip (project packaging)
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
SPI read / write flash principle + complete code
包 类 包的作用域
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
MacBook installation postgresql+postgis
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...