1. 程式人生 > >c: 說說sizeof 和 static 關鍵字

c: 說說sizeof 和 static 關鍵字

sizeof for 陣列

理想的你,不能用 sizeof求陣列的元素個數

實際的你,應該使用下方的方法,來求陣列的元素個數:

int c1=sizeof(a1)/sizeof(char);//總長度/單個元素的長度  char型
int c2=sizeof(a2)/sizeof(a2[0]);//總長度/第一個元素的長度 int型 

static 關鍵字

三個字: “隱蔽性”

上程式碼

#include <stdio.h>
int fun(void)
{
static int count = 10; // 此語句只在函式第一次呼叫時執行,後續函式呼叫此變數的初始值為上次呼叫後的值,每次呼叫後儲存空間不釋放
return count--;// 這個count 對下面的count不衝突,main 函式不可見我
}
int count = 1;
int main(void)
{
	printf("global\t\tlocal static\n");
	for(count = 0; count <= 11; ++count)
		printf("%d\t\t%d\n", count, fun());
	return 0;
}

在這裡插入圖片描述