sizeof 运算符

使用类型作为操作数

计算给定类型的对象的类型 size_t 的字节大小。需要围绕类型的括号。

printf("%zu\n", sizeof(int)); /* Valid, outputs the size of an int object, which is platform-dependent. */
printf("%zu\n", sizeof int); /* Invalid, types as arguments need to be surrounded by parentheses! */

使用表达式作为操作数

计算给定表达式类型的对象的 size_t 类型的字节大小。表达式本身未被评估。括号不是必需的; 但是,因为给定的表达式必须是一元的,所以最好始终使用它们。

char ch = 'a';
printf("%zu\n", sizeof(ch)); /* Valid, will output the size of a char object, which is always 1 for all platforms. */
printf("%zu\n", sizeof ch);  /* Valid, will output the size of a char object, which is always 1 for all platforms. */