当前位置:网站首页>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 .
边栏推荐
- CSDN body auto generate directory
- Pointer function (basic)
- 机器学习 --- 神经网络
- [untitled]
- MySQL: view with subquery in the from clause limit
- Debug insights
- A solution to the problem that variables cannot change dynamically when debugging in keil5
- 防护电路中的元器件
- level18
- [PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
猜你喜欢
![[phantom engine UE] the difference between running and starting, and the analysis of common problems](/img/e2/49d6c4777c12e9f4e3f8b6ca6db41c.png)
[phantom engine UE] the difference between running and starting, and the analysis of common problems

About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture

Observable time series data downsampling practice in Prometheus

WeNet:面向工业落地的E2E语音识别工具

托管式服务网络:云原生时代的应用体系架构进化

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)

Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics

【thingsboard】替换首页logo的方法

Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode

Cookie learning diary 1
随机推荐
解密函数计算异步任务能力之「任务的状态及生命周期管理」
CSDN body auto generate directory
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
【虚幻引擎UE】实现测绘三脚架展开动画制作
程序员应该怎么学数学
函数(基本:参数,返回值)
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
OWASP top 10 vulnerability Guide (2021)
A application wakes up B should be a fast method
Decimal to hexadecimal
2022-2028 global and Chinese video coding and transcoding Market Research Report
Common features of ES6
Debug insights
Seven join join queries of MySQL
[phantom engine UE] the difference between running and starting, and the analysis of common problems
函数(易错)
如何进行「小步重构」?
Sword finger offer 07 Rebuild binary tree
How to force activerecord to reload a class- How do I force ActiveRecord to reload a class?
This is an age of uncertainty