关于强制转换:” sizeof(int)”中的”(int)”是类型转换运算符还是某些特殊情况参数? [C]

Is the “(int)” in “sizeof (int)” a typecast operator or some special case parameter? [C]

本问题已经有最佳答案,请猛点这里访问。

我一直认为C表达式sizeof (int)就像函数一样工作,括号内的任何内容都像参数一样传递。由于int(或任何其他类型说明符)实际上是一个关键字而不是一个可以传递的对象,因此我认为这只是某种特殊情况,被硬编码到编译器中。

但是我最近发现,如果在变量上使用sizeof,则可以省略括号(sizeof var),它将编译并运行良好。由于sizeof实际上是运算符本身,而不是函数(并且运算符不需要括号),并且类型说明符是唯一需要括号的内容,因此我想知道它们是否真的是类型说明符,或者它们是否是类型说明符? sizeof实际上以某种方式重新使用了强制类型转换运算符,以查找相应类型的大小。


如果操作数是数据类型,则

()sizeof运算符一起使用。
C11:6.5.3.4(p2):

The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of the operand.