当前位置:网站首页>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 .
边栏推荐
- You Li takes you to talk about C language 7 (define constants and macros)
- PHP reads the INI file and writes the modified content
- 【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
- Uncover the seven quirky brain circuits necessary for technology leaders
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- A應用喚醒B應該快速方法
- 概率论与数理统计考试重点复习路线
- How to get the first few pieces of data of each group gracefully
- 10 programming habits that web developers should develop
- MacBook installation postgresql+postgis
猜你喜欢

函数(基本:参数,返回值)

揭秘技术 Leader 必备的七大清奇脑回路

假设检验——《概率论与数理统计》第八章学习笔记

SPI read / write flash principle + complete code

TPG x AIDU|AI领军人才招募计划进行中!

可观测|时序数据降采样在Prometheus实践复盘

Label exchange experiment

【虚幻引擎UE】打包报错出现!FindPin错误的解决办法

How can CIOs use business analysis to build business value?

Sword finger offer 04 Search in two-dimensional array
随机推荐
[crampon game] MC tutorial - first day of survival
Interview related high-frequency algorithm test site 3
指针函数(基础)
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
A solution to the problem that variables cannot change dynamically when debugging in keil5
What are the building energy-saving software
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
CSDN正文自动生成目录
Here comes the Lantern Festival red envelope!
Hexadecimal to octal
Neural networks and deep learning Chapter 2: machine learning overview reading questions
网络安全-记录web漏洞修复
Leetcode hot topic Hot 100 day 33: "subset"
Practice | mobile end practice
Sequence diagram of single sign on Certification Center
3 minutes learn to create Google account and email detailed tutorial!
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
Learning notes 8
直播预告 | 容器服务 ACK 弹性预测最佳实践