C 中的编译时运算符
我只熟悉 C 语言中的一个编译时运算符 - sizeof
。作为一名程序员,还有其他我应该注意的吗?
I'm familiar with only one compile time operator in C - sizeof
. Are there any others that I as a programmer should be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只知道
sizeof
,尽管在 C99 中,sizeof 无法在编译时为可变长度数组 (VLA) 完成。Only
sizeof
that I'm aware, although in C99 sizeof cannot be done at compile time for variable length arrays (VLAs).我认为您正在掌握但缺少描述的术语是常量表达式或整数常量表达式。如果某些运算符的操作数是常量,则它们的结果可以是(整数)常量表达式,并且正如您所指出的,即使其操作数不是常量,只要它不是常量,
sizeof
的结果也可以是(整数)常量表达式。一个VLA。请参阅 C99 中的 6.6:http://port70.net/~nsz/c/ c99/n1256.html#6.6
I think what you're grasping for but missing the terminology to describe is a constant expression or integer constant expression. The results of certain operators can be (integer) constant expressions if their operands are, and as you've pointed out, the result of
sizeof
can be even if its operand is not constant as long as it's not a VLA. See 6.6 in C99:http://port70.net/~nsz/c/c99/n1256.html#6.6
新的 C 标准还具有与
sizeof
类似的_Alignof
功能。The new C standard also has
_Alignof
that works similar tosizeof
.虽然严格来说不是一个运算符,但有
offsetof
,它是stddef.h
中定义的宏。它用于获取struct
中字段成员的偏移量。例如:Although not strictly an operator, there is
offsetof
, a macro defined instddef.h
. It is used to get the offset of a field member in astruct
. For example: