当前位置:网站首页>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)
- The remainder operation is a hash function
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 【UNIAPP】系统热更新实现思路
- Machine learning -- neural network
- 介绍汉明距离及计算示例
- Raki's notes on reading paper: code and named entity recognition in stackoverflow
- Wenet: E2E speech recognition tool for industrial implementation
- After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
- Debug insights
猜你喜欢
![[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

A survey of automatic speech recognition (ASR) research

【thingsboard】替换首页logo的方法

Label exchange experiment

【虚幻引擎UE】运行和启动的区别,常见问题分析

How to get the first few pieces of data of each group gracefully

【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录

Invalid bound statement (not found) in idea -- problem solving

美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败

Raki's notes on reading paper: code and named entity recognition in stackoverflow
随机推荐
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
电源管理总线 (PMBus)
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Interview related high-frequency algorithm test site 3
Neural networks and deep learning Chapter 3: linear model reading questions
如何进行「小步重构」?
机器学习 --- 决策树
Ffmepg usage guide
2022-2028 global and Chinese FPGA prototype system Market Research Report
[finebi] the process of making custom maps using finebi
Burpsuite grabs app packets
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
Discussion on the dimension of confrontation subspace
Leetcode hot topic Hot 100 day 33: "subset"
Hexadecimal to decimal
TPG x AIDU|AI领军人才招募计划进行中!
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
A應用喚醒B應該快速方法
【thingsboard】替换首页logo的方法
2022-2028 global and Chinese equipment as a Service Market Research Report