C++變量存儲的生命周期與作用域?qū)嵗a精講
auto類型:非靜態(tài)的局部變量存儲類型都是auto,這些數(shù)據(jù)存儲在棧區(qū),不初始化變量的值時隨機(jī)的。C++中的auto還可以自動推導(dǎo)類型。生命周期:塊內(nèi) 作用域:塊內(nèi)
程序:
#include <stdio.h> void test(void); int main() { // auto存儲類型 auto b = 13; // C++新功能,auto自動推導(dǎo)類型 int a = 12; // auto存儲類型的局部變量,存儲在函數(shù)棧幀中 { int c = 11; printf("%d\n",a); printf("%d\n",c); } test(); printf("%d\n",a); return 0; } void test(void) { int d = 13; // auto存儲類型的局部變量,存儲在函數(shù)棧幀中 printf("%d\n",d); }
static類型:static靜態(tài)存儲類型的變量,可以作為局部變量和全局變量。作為全局變量的時候不能被外部文件所訪問,靜態(tài)變量只初始化一次,存儲在靜態(tài)區(qū)中。也可以用來修飾函數(shù),這樣外部文件無法調(diào)用該函數(shù)。生命周期:整個程序 作用域:全局靜態(tài)文件內(nèi)、局部塊內(nèi)
程序:局部靜態(tài)變量
#include <stdio.h> #include <windows.h> void test(void); int main() { test(); test(); // printf("%d", a); static作為局部變量,外面是訪問不了的 system("pause"); return 0; } // 局部靜態(tài)變量,存儲在靜態(tài)區(qū)中 void test(void) { static int a = 11; // 只會被初始化一次 a++; printf("%d\n", a); }
程序:全局靜態(tài)變量
#include <stdio.h> #include <windows.h> void test(void); static int b = 33; // 全局靜態(tài)變量,外部文件無法訪問,存儲在靜態(tài)區(qū)中 int main() { test(); printf("%d\n", b); system("pause"); return 0; } void test(void) { printf("%d\n", b); }
register類型:寄存器變量,存儲在cpu中不在內(nèi)存中,所以沒有地址。可以加快計算機(jī)訪問。但是在C++中如果一定要去訪問寄存器變量那么寄存器變量會被降級成普通變量。寄存器變量不能作為全局變量
程序:
#include <stdio.h> // register int b = 12; 寄存器變量沒法作為全局變量 int main() { // register變量沒有地址 register int a = 12; printf("%d",a); printf("%p", &a); // 強(qiáng)制訪問register變量,那么這個變量會變?yōu)閍uto類型 for(register int i=0; i<1000; i++){ // 加快運(yùn)行速度寫法,但是沒必要 } return 0; }
extern類型:可以訪問外部文件中的全局變量,只要在本文件中的變量前加上extern表示他是個外部變量。
程序:
extern.h
#ifndef _EXTER_H_ #define _EXTER_H_ #include <stdio.h> void test1(); #endif
extern_test.cpp
#include "exter.h" int c = 44; int d = 55; // 這里不要寫extern int d;這是錯誤的 ,也不要寫成extern int d=55這個是對的但是不推薦 void test1() { printf("extern_test_c_addr:%p\n", &c); printf("extern_test_d_addr:%p\n", &d); }
man.cpp
#include <stdio.h> #include <windows.h> #include "exter.h" void test(void); extern int d; // extern拿到其他文件變量并作為本文件的全局變量 int main() { // extern拿到其他文件變量并作為本文件的局部變量 extern int c; printf("c=%d\n",c); c = 12; printf("c=%d\n",c); printf("d=%d\n",c); test(); test1(); printf("extern_test_c_addr:%p\n", &c); printf("main_d_addr:%p\n", &d); system("pause"); return 0; } void test(void) { printf("test d=%d\n",d); //printf("c=%d\n", c); 局部變量訪問不了 }
到此這篇關(guān)于C++變量存儲的生命周期與作用域?qū)嵗a精講的文章就介紹到這了,更多相關(guān)C++生命周期與作用域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vscode 配置 C/C++編譯環(huán)境(完整教程)
這篇文章主要介紹了vscode 配置 C/C++編譯環(huán)境(完整教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持(下)
這篇文章主要介紹的是 C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持的相關(guān)資料,承接前文 現(xiàn)代 C++ 對多線程/并發(fā)的支持,接下來我們看看回發(fā)生什么吧2021-10-10C++實(shí)現(xiàn)簡易圖書館管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡易圖書館管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03深入學(xué)習(xí)C語言中的函數(shù)指針和左右法則
這篇文章主要介紹了深入學(xué)習(xí)C語言中的函數(shù)指針和左右法則,左右法則是一種常用的C指針聲明,需要的朋友可以參考下2015-08-08