sizeof是C語言中保留關鍵字,也可以認為是壹種運算符,單目運算符。常見的使用方式:
int a=10;
int arr=[1,2,3];
char str[]="hello";
int len_a = sizeof(a);
int len_arr = sizeof(arr);
int len_str = sizeof(str)
printf("len_a=%d,len_arr=%d,len_str=%d\n",len_a,len_arr,len_str)
結果是:len_a=4,len_arr=12,len_str=6
擴展資料:
在 Pascal 語言中,sizeof() 是壹種內存容量度量函數,功能是返回壹個變量或者類型的大小(以字節為單位)。
在 C 語言中,sizeof() 是壹個判斷數據類型或者表達式長度的運算符。
在Pascal 語言與C語言中,對 sizeof() 的處理都是在編譯階段進行。
sizeof是C/C++中的壹個操作符(operator),簡單的說其作用就是返回壹個對象或者類型所占的內存字節數。
MSDN上的解釋為:
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types). This keyword returns a value of type size_t.
參考資料:
百度百科-sizeof