sizeof:运算符还是函数?
可能的重复:
为什么 sizeof 是一个运算符?
为什么 sizeof
应该是成为 C 语言中的运算符,&不是函数?
它的用法看起来与函数调用类似,如 sizeof(int)
中所示。
它应该是某种伪函数吗?
Possible Duplicate:
Why is sizeof an operator?
Why is sizeof
supposed to be an operator in C, & not a function?
Its usage seems similar to that of a function call, as in sizeof(int)
.
Is it supposed to be some kind of a pseudo-function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为它不是一个函数;不可能编写一个可以将类型作为参数并返回其大小的函数!另请注意,
sizeof
是在编译时实现的,如果您在变量上使用它,则不需要括号。Because it's not a function; it's not possible to write a function that can take a type as an argument and return its size! Note also that
sizeof
is implemented at compile-time, and that the parentheses aren't necessary if you're using it on a variable.函数调用是在运行时评估的。
sizeof
必须在编译时求值(尽管我相信 C99 对此引入了一些例外)A function call is evaluated at runtime.
sizeof
must be evaluated at compile time (though C99 introduces some exceptions to this, I believe)C 中的函数无法执行 sizeof 需要执行的操作。
根据定义,函数在运行时分配空间,并且只能对数据进行操作。
虽然 sizeof 也对数据类型进行操作,但这些数据类型是特定于编译器的。根据设计,函数不知道如何解释数据类型“int”并询问编译器其大小。
在 C99 之前,sizeof 完全是编译时的,但在新标准中,它也可以用于在运行时获取可变长度数组的信息。
A function in C cannot do what sizeof needs to do.
A function by definition is allocated space at runtime and can operate on data only.
While sizeof operates on datatypes as well, which are compiler specific. A function does not know how to interpret the datatype "int" and ask the compiler for its size, by design.
Before C99 sizeof was totally compile-time, but in the new standard, it can be used to get information at runtime as well, for variable-length arrays.
函数是您可以在运行时调用的东西。 sizeof 只有编译器才能理解。
function is something you can call at runtime. sizeof is only understood by the compiler.
sizeof 在编译时而不是执行时产生影响。当您输入诸如 sizeof(int) 之类的类型时,您只需要 ( ) 但如果 'i' 是 int 类型,您可以执行 sizeof i
sizeof has its effects at complite time not execution time. You only need the ( ) when you enter a type such as sizeof(int) but if 'i' is of type int you could do sizeof i