演员与后缀 [英] cast vs. suffixes

查看:65
本文介绍了演员与后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你想明确说明一个整数常量的类型,你

基本上有两个选项。您可以使用显式强制转换,例如:

(unsigned int)1234

或您使用UL后缀,如:

1234UL

是否 - 通常 - 使用演员表中的任何语义差异

与后缀相比?

--Ralf

If you want to explicitly state the type of an integer constant, you
basically have two options. Either you use an explicit cast, as in:
(unsigned int) 1234
or you use the UL suffixes, as in:
1234UL
Are there -- in general -- any semantic differences in using casts
versus suffixes?
--Ralf

推荐答案

Ralf写道:
如果你想明确说明一个整数常量的类型,你基本上有两种选择。您可以使用显式强制转换,如下所示:
(unsigned int)1234


这可以在编译器选择时在

运行时。我仍然期望一个好的优化者在

编译时这样做。所以,如果你错误输入

你的常数,你可能会得到令人讨厌的错误。

或你使用UL后缀,如:
1234UL


这总是在编译时完成。在这里,错误的常量

在体外仍被捕获。

是否 - 通常情况下 - 使用强制转换中的语义差异
与后缀相比?
--Ralf
If you want to explicitly state the type of an integer constant, you
basically have two options. Either you use an explicit cast, as in:
(unsigned int) 1234
This, at compiler''s choice, can be left to be evaluated at
run-time. I''d still expect a good optimiser to do it at
compile-time. So, potentially, you get nasty bugs if you mistype
your constants.
or you use the UL suffixes, as in:
1234UL
This is always done at compile time. Here, mistyped constants
are caught while still in vitro.
Are there -- in general -- any semantic differences in using casts
versus suffixes?
--Ralf




目前可能还有其他一些影响我的事情。


干杯

弗拉基米尔

-

我的电子邮件地址是真的,我看了。



There may also be other implications which escape me at the moment.

Cheers

Vladimir
--
My e-mail address is real, and I read it.





Ralf在01/20/06 12:52写道:


Ralf wrote On 01/20/06 12:52,:
如果要显式声明整数的类型不断,你基本上有两个选择。您可以使用显式强制转换,例如:
(unsigned int)1234
或者您使用UL后缀,如:
1234UL
是否 - 通常 - 使用演员表与后缀相比的任何语义差异?
If you want to explicitly state the type of an integer constant, you
basically have two options. Either you use an explicit cast, as in:
(unsigned int) 1234
or you use the UL suffixes, as in:
1234UL
Are there -- in general -- any semantic differences in using casts
versus suffixes?




是的。 `(unsigned int)1234''不是常量:它是

一个表达式,由运算符`(unsigned int)组成''

应用于常量操作数` 1234 ''。

表达式的结果是'unsigned int'',值为

一千二百三十四。

`1234UL''是常数。它的类型是`unsigned long''

,它的值是一千二百三十四。

(请注意,这个常量的类型与

早期表达式的类型。)


一个地方可以产生一个容易观察到的差异

在预处理器中。由于预处理器在编译的早期阶段运行

之前类型存在,

它无法评估演员操作符 - 因为演员表全部是
$关于类型转换的b $ b并且还没有任何类型,

预处理器无法对它们做任何事情。


考虑以下代码:


#include< stdio.h>


int main(无效){

#if 1234UL> 0

put(#1:正如预期的那样);

#else

puts(#1:难以置信! );

#endif


#if(unsigned int)1234> 0

put(#2:正如预期的那样);

#else

puts(#2:难以置信! );

#endif


返回0;

}


无需编译并运行它,你能预测

输出吗? (警告:我问了一个技巧问题。)


-
Er ********* @ sun.com



Yes. `(unsigned int)1234'' is not a constant: it is
an expression consisting of the operator `(unsigned int)''
applied to the constant operand `1234''. The type of the
expression''s result is `unsigned int'' and the value is
one thousand two hundred thirty-four.

`1234UL'' is a constant. Its type is `unsigned long''
and its value is one thousand two hundred thirty-four.
(Note that the type of this constant is not the same as
the type of the earlier expression.)

One place this makes an easily-observable difference
is in the preprocessor. Since the preprocessor operates
at an early stage of compilation before types "exist,"
it cannot evaluate cast operators -- since casts are all
about type conversions and there aren''t any types yet,
there''s really nothing the preprocessor can do with them.

Consider the following code:

#include <stdio.h>

int main(void) {
#if 1234UL > 0
puts ("#1: as expected");
#else
puts ("#1: unbelievable!");
#endif

#if (unsigned int)1234 > 0
puts ("#2: as expected");
#else
puts ("#2: unbelievable!");
#endif

return 0;
}

Without compiling and running it, can you predict the
output? (Warning: I''ve asked a trick question.)

--
Er*********@sun.com


Ralf写道:

如果你想明确说明一个整数常量的类型,你基本上有两个选择。您可以使用显式强制转换,例如:
(unsigned int)1234
或者您使用UL后缀,如:
1234UL
是否 - 通常 - 使用强制转换与后缀的任何语义差异?

If you want to explicitly state the type of an integer constant, you
basically have two options. Either you use an explicit cast, as in:
(unsigned int) 1234
or you use the UL suffixes, as in:
1234UL
Are there -- in general -- any semantic differences in using casts
versus suffixes?




具有16位整数的系统会发生什么:


(长)65537




65537L


-

+ ------------------------- + ------------------ - + ----------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>



What happens on a system with 16-bit integers with:

(long)65537

versus

65537L

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


这篇关于演员与后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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