为什么将sizeof视为运算符? [英] Why is sizeof considered an operator?

查看:103
本文介绍了为什么将sizeof视为运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么sizeof被认为是运算符而不是函数?

Why is sizeof considered an operator and not a function?

要成为运营商,必须具备哪些财产?

What property is necessary to qualify as an operator?

推荐答案

由于C标准如此规定,因此它获得了唯一的表决权.

Because the C standard says so, and it gets the only vote.

后果:

  • sizeof的操作数可以是带括号的类型sizeof (int),而不是对象表达式.
  • 括号是不必要的:int a; printf("%d\n", sizeof a);非常好.人们经常看到它们,首先是因为它们是类型转换表达式的一部分,其次是因为sizeof的优先级很高,因此sizeof a + bsizeof (a+b)不同.但是它们不是sizeof调用的一部分,而是操作数的一部分.
  • 您不能使用sizeof的地址.
  • 作为sizeof操作数的表达式不会在运行时求值(sizeof a++不会修改a).
  • 作为sizeof操作数的表达式可以具有除void或函数类型以外的任何类型.确实,这就是sizeof的重点.
  • The operand of sizeof can be a parenthesised type, sizeof (int), instead of an object expression.
  • The parentheses are unnecessary: int a; printf("%d\n", sizeof a); is perfectly fine. They're often seen, firstly because they're needed as part of a type cast expression, and secondly because sizeof has very high precedence, so sizeof a + b isn't the same as sizeof (a+b). But they aren't part of the invocation of sizeof, they're part of the operand.
  • You can't take the address of sizeof.
  • The expression which is the operand of sizeof is not evaluated at runtime (sizeof a++ does not modify a).
  • The expression which is the operand of sizeof can have any type except void, or function types. Indeed, that's kind of the point of sizeof.

所有这些点上的功能都会有所不同.函数和一元运算符之间可能还存在其他差异,但是我认为这足以说明为什么sizeof不能成为函数,即使有理由希望它成为函数.

A function would differ on all those points. There are probably other differences between a function and a unary operator, but I think that's enough to show why sizeof could not be a function even if there was a reason to want it to be.

这篇关于为什么将sizeof视为运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆