# static ###### tags: `C basics` ### Reference >致謝 >https://www.itread01.com/content/1547705175.html ### Basics static的作用有三種,分別是: 1. 是隱藏功能,對於static修飾的函式和全域性變數而言 2. 是保持永續性功能,對於static修飾的區域性變數而言。 2. 因為存放在靜態區,全域和區域static變數,都預設初始化為0 當我們同時編譯多個檔案時,所有未加static字首的全域性變數和函式都具有全域性可見性。例如我們要同時編譯兩個原始檔,一個是a.c,另一個是main.c。 下面是a.c的內容 ```c= char a = 'A'; // global variable void msg(){ printf("I am a.c\n"); } ``` 下面是main.c的內容 ```c= int main(void) { extern char a; // extern variable must be declared before use printf("%c ", a); msg(); return 0; } ``` 程式的執行結果是: A I am a.c **結果顯示對於main.c,a.c原始檔的東西是可見的** 如果加了static,就會對其它原始檔隱藏,例如在a和msg的定義前加上static,main.c就看不到它們。利用這一特性可以在不同的檔案中定義同名函式和同名變數,而不必擔心命名衝突。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up