# C 存儲類
存儲類定義 C 程序中變量/函數的范圍(可見性)和生命周期。這些說明符放置在它們所修飾的類型之前。下面列出 C 程序中可用的存儲類:
* auto
* register
* static
* extern
## auto 存儲類
**auto** 存儲類是所有局部變量默認的存儲類。
```
{
int mount;
auto int month;
}
```
上面的實例定義了兩個帶有相同存儲類的變量,auto 只能用在函數內,即 auto 只能修飾局部變量。
## register 存儲類
**register** 存儲類用于定義存儲在寄存器中而不是 RAM 中的局部變量。這意味著變量的最大尺寸等于寄存器的大小(通常是一個詞),且不能對它應用一元的 '&' 運算符(因為它沒有內存位置)。
```
{
register int miles;
}
```
寄存器只用于需要快速訪問的變量,比如計數器。還應注意的是,定義 'register' 并不意味著變量將被存儲在寄存器中,它意味著變量可能存儲在寄存器中,這取決于硬件和實現的限制。
## static 存儲類
**static** 存儲類指示編譯器在程序的生命周期內保持局部變量的存在,而不需要在每次它進入和離開作用域時進行創建和銷毀。因此,使用 static 修飾局部變量可以在函數調用之間保持局部變量的值。
static 修飾符也可以應用于全局變量。當 static 修飾全局變量時,會使變量的作用域限制在聲明它的文件內。
在 C 編程中,當 **static** 用在類數據成員上時,會導致僅有一個該成員的副本被類的所有對象共享。
```
#include <stdio.h>
/* 函數聲明 */
void func(void);
static int count = 5; /* 全局變量 */
main()
{
while(count--)
{
func();
}
return 0;
}
/* 函數定義 */
void func( void )
{
static int i = 5; /* 局部靜態變量 */
i++;
printf("i is %d and count is %d\n", i, count);
}
```
可能您現在還無法理解這個實例,因為我已經使用了函數和全局變量,這兩個概念目前為止還沒進行講解。即使您現在不能完全理解,也沒有關系,后續的章節我們會詳細講解。當上面的代碼被編譯和執行時,它會產生下列結果:
```
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
```
## extern 存儲類
**extern** 存儲類用于提供一個全局變量的引用,它對所有的程序文件都是可見的。當您使用 'extern' 時,對于無法初始化的變量,會把變量名指向一個之前定義過的存儲位置。
當您有多個文件且定義了一個可以在其他文件中使用的全局變量或函數時,可以在其他文件中使用 _extern_ 來得到已定義的變量或函數的引用。可以這么理解,_extern_ 是用來在另一個文件中聲明一個全局變量或函數。
extern 修飾符通常用于當有兩個或多個文件共享相同的全局變量或函數的時候,如下所示:
**第一個文件:main.c**
```
#include <stdio.h>
int count ;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
```
**第二個文件:support.c**
```
#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %d\n", count);
}
```
在這里,第二個文件中的 _extern_ 關鍵字用于聲明已經在第一個文件 main.c 中定義的 _count_。現在 ,編譯這兩個文件,如下所示:
```
$gcc main.c support.c
```
這會產生 **a.out** 可執行程序,當程序被執行時,它會產生下列結果:
```
5
```
- C語言教程
- C 簡介
- C 環境設置
- C 程序結構
- C 基本語法
- C 數據類型
- C 變量
- C 常量
- C 存儲類
- C 運算符
- C 判斷
- C 循環
- C 函數
- C 作用域規則
- C 數組
- C 指針
- C 字符串
- C 結構體
- C 共用體
- C 位域
- C typedef
- C 輸入 & 輸出
- C 文件讀寫
- C 預處理器
- C 頭文件
- C 強制類型轉換
- C 錯誤處理
- C 遞歸
- C 可變參數
- C 內存管理
- C 命令行參數
- C語言參考
- C 標準庫 - <assert.h>
- C 庫宏 - assert()
- C 標準庫 - <ctype.h>
- C 庫函數 - isalnum()
- C 庫函數 - isalpha()
- C 庫函數 - iscntrl()
- C 庫函數 - isdigit()
- C 庫函數 - isgraph()
- C 庫函數 - islower()
- C 庫函數 - isprint()
- C 庫函數 - ispunct()
- C 庫函數 - isspace()
- C 庫函數 - isupper()
- C 庫函數 - isxdigit()
- C 標準庫 - <errno.h>
- C 庫宏 - errno
- C 庫宏 - EDOM
- C 庫宏 - ERANGE
- C 標準庫 - <float.h>
- C 標準庫 - <limits.h>
- C 標準庫 - <locale.h>
- C 庫函數 - setlocale()
- C 庫函數 - localeconv()
- C 標準庫 - <math.h>
- C 庫函數 - acos()
- C 庫函數 - asin()
- C 庫函數 - atan()
- C 庫函數 - atan2()
- C 庫函數 - cos()
- C 庫函數 - cosh()
- C 庫函數 - sin()
- C 庫函數 - sinh()
- C 庫函數 - tanh()
- C 庫函數 - exp()
- C 庫函數 - frexp()
- C 庫函數 - ldexp()
- C 庫函數 - log()
- C 庫函數 - log10()
- C 庫函數 - modf()
- C 庫函數 - pow()
- C 庫函數 - sqrt()
- C 庫函數 - ceil()
- C 庫函數 - fabs()
- C 庫函數 - floor()
- C 庫函數 - fmod()
- C 標準庫 - <setjmp.h>
- C 庫宏 - setjmp()
- C 庫函數 - longjmp()
- C 標準庫 - <signal.h>
- C 庫函數 - signal()
- C 庫函數 - raise()
- C 標準庫 - <stdarg.h>
- C 庫宏 - va_start()
- C 庫宏 - va_arg()
- C 庫宏 - va_end()
- C 標準庫 - <stddef.h>
- C 庫宏 - NULL
- C 庫宏 - offsetof()
- C 標準庫 - <stdio.h>
- C 庫函數 - fclose()
- C 庫函數 - clearerr()
- C 庫函數 - feof()
- C 庫函數 - ferror()
- C 庫函數 - fflush()
- C 庫函數 - fgetpos()
- C 庫函數 - fopen()
- C 庫函數 - fread()
- C 庫函數 - freopen()
- C 庫函數 - fseek()
- C 庫函數 - fsetpos()
- C 庫函數 - ftell()
- C 庫函數 - fwrite()
- C 庫函數 - remove()
- C 庫函數 - rename()
- C 庫函數 - rewind()
- C 庫函數 - setbuf()
- C 庫函數 - tmpfile()
- C 庫函數 - tmpnam()
- C 庫函數 - fprintf()
- C 庫函數 - printf()
- C 庫函數 - sprintf()
- C 庫函數 - vfprintf()
- C 庫函數 - vprintf()
- C 庫函數 - vsprintf()
- C 庫函數 - fscanf()
- C 庫函數 - scanf()
- C 庫函數 - sscanf()
- C 庫函數 - fgetc()
- C 庫函數 - fgets()
- C 庫函數 - fputc()
- C 庫函數 - fputs()
- C 庫函數 - getc()
- C 庫函數 - getchar()
- C 庫函數 - gets()
- C 庫函數 - putc()
- C 庫函數 - putchar()
- C 庫函數 - puts()
- C 庫函數 - ungetc()
- C 庫函數 - perror()
- C 標準庫 - <stdlib.h>
- C 庫函數 - atof()
- C 庫函數 - atoi()
- C 庫函數 - atol()
- C 庫函數 - strtod()
- C 庫函數 - strtol()
- C 庫函數 - strtoul()
- C 庫函數 - calloc()
- C 庫函數 - free()
- C 庫函數 - malloc()
- C 庫函數 - realloc()
- C 庫函數 - abort()
- C 庫函數 - atexit()
- C 庫函數 - exit()
- C 庫函數 - getenv()
- C 庫函數 - system()
- C 庫函數 - bsearch()
- C 庫函數 - qsort()
- C 庫函數 - abs()
- C 庫函數 - div()
- C 庫函數 - labs()
- C 庫函數 - ldiv()
- C 庫函數 - rand()
- C 庫函數 - srand()
- C 庫函數 - mblen()
- C 庫函數 - mbstowcs()
- C 庫函數 - mbtowc()
- C 庫函數 - wcstombs()
- C 庫函數 - wctomb()
- C 標準庫 - <string.h>
- C 庫函數 - memchr()
- C 庫函數 - memcmp()
- C 庫函數 - memcpy()
- C 庫函數 - memmove()
- C 庫函數 - memset()
- C 庫函數 - strcat()
- C 庫函數 - strncat()
- C 庫函數 - strchr()
- C 庫函數 - strcmp()
- C 庫函數 - strncmp()
- C 庫函數 - strcoll()
- C 庫函數 - strcpy()
- C 庫函數 - strncpy()
- C 庫函數 - strcspn()
- C 庫函數 - strerror()
- C 庫函數 - strlen()
- C 庫函數 - strpbrk()
- C 庫函數 - strrchr()
- C 庫函數 - strspn()
- C 庫函數 - strstr()
- C 庫函數 - strtok()
- C 庫函數 - strxfrm()
- C 標準庫 - <time.h>
- C 庫函數 - asctime()
- C 庫函數 - clock()
- C 庫函數 - ctime()
- C 庫函數 - difftime()
- C 庫函數 - gmtime()
- C 庫函數 - localtime()
- C 庫函數 - mktime()
- C 庫函數 - strftime()
- C 庫函數 - time()
- 免責聲明