在C/C ++中定义类型化的常数 [英] defining typed constant numbers in C/C++
问题描述
在C/C ++中,说(1U)
与((unsigned int)1)
之间有区别吗?我更喜欢第二个,但是我担心第二个可能在运行时进行类型转换(即额外的cpu周期),而第一个在编译时会得到正确的类型.谢谢.
In C/C++, is there a difference between saying (1U)
vs. ((unsigned int)1)
? I prefer the second one, but I am concerned that the second one may be type-cast at run time (i.e. extra cpu cycles), whereas the first one gets the correct type at compilation. Thanks.
推荐答案
它们不是等效的. 1U
在#if
预处理指令中有效. (unsigned int)1
是预处理器级别的语法错误.但是,您可以将其设置为(unsigned)+1
,并且在预处理器中将是有效的,但是仅由于一条晦涩的规则,很少有人知道.
They're not equivalent. 1U
is valid in #if
preprocessing directives. (unsigned int)1
is a syntax error at the preprocessor level. You could however make it (unsigned)+1
and it would be valid in the preprocessor, but only because of an obscure rule few people know..
这篇关于在C/C ++中定义类型化的常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!