在C/C ++中定义类型化的常数 [英] defining typed constant numbers in C/C++

查看:88
本文介绍了在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屋!

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