stdint类型和转换...... [英] stdint types and conversion...
问题描述
大家好,
INT8_C,INT16_C等名称的转换宏在
stdint.h中定义,将其参数转换为合适的表示形式对于
它们对应的类型,即int8_t,int16_t等。
我的问题是:
1.转换宏应该是甚至在从相同类型的
变量中分配时使用?
2.为什么没有为int_fast8_t等提供这样的宏以及
int_least8_t等?可以为他们分配其他类型的值吗?
没有大小不匹配并期望发生正确的事情?
谢谢。
Hello all,
Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?
Thanks.
推荐答案
santosh写道:
santosh wrote:
大家好,
转换宏名为INT8_C,INT16_C等等,在stdint.h中定义,将它们的参数转换为适当的表示形式,用于它们对应的类型,即int8_t,int16_t等。
这些不是* c * onversion宏,它们用于声明适当类型的* c * onstants
。 "转换"暗示某些东西正在被转换,
但事实并非如此;常量将具有适当的类型。您的
其他问题也来自于这种误解。
我的问题是:
1.即使从
变量中分配转换宏也应该使用相同类型的?
不可以。事实上,你必须*不*使用它们:
"这些宏的任何实例中的参数都应该是十进制,八进制或
十六进制常量(如6.4.4.1中所定义),其值不是
超过相应类型的限制。 [7.8.14,第2段]。
任何类型的变量都不是(必须是)常数。
2.为什么不这样为int_fast8_t等提供的宏以及
int_least8_t等?可以为他们分配其他类型的值吗?
没有大小不匹配并期望发生正确的事情?
Hello all,
Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion" implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
No. In fact, you must *not* use them:
"The argument in any instance of these macros shall be a decimal, octal, or
hexadecimal constant (as defined in 6.4.4.1) with a value that does not
exceed the limits for the corresponding type." [7.8.14, paragraph 2].
A variable of any kind is not (required to be) a constant.
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?
编号通常的规则适用:你需要一个演员表价值可能不合适,
并且你有责任确保它们合适。 (一个粗略的过度简化
的实际规则,当然,看看有关C的隐含和
显式转换的任何好书。)
S.
No. The usual rules apply: you need a cast for values that might not fit,
and it''s your responsibility to ensure they fit. (A gross oversimplification
of the actual rules, of course, see any good book on C about implicit and
explicit conversion.)
S.
Skarmander写道:
Skarmander wrote:
santosh写道:
santosh wrote:
大家好,
INT8_C,INT16_C等名称的转换宏在
stdint.h中定义,将其参数转换为适当的表示形式,即相应的类型,即int8_t,int16_t等。
Hello all,
Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
这些不是* c * onversion宏,它们用于声明相应类型的* c * onstants。 "转换"暗示某些东西正在转变,
并非如此;常量将具有适当的类型。您的其他问题也来自于这种误解。
These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion" implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.
如果我理解正确的话,在分配时将使用宏
字面常量它们各自类型的变量值?
类似于:
....
int8_t i8 = INT8_C(-112 );
....
If I understand correctly the macros are to be used when assigning
literal constant values to variables of their respective types?
Something like:
....
int8_t i8 = INT8_C(-112);
....
我的问题是:
1.是否应使用转换宏即使从相同类型的变量中分配?
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
否。实际上,你必须*不*使用它们:
任何参数这些宏的实例应为十进制,八进制或
十六进制常量(如6.4.4.1中所定义),其值不会超过相应类型的限制。 [7.8.14,第2段]。
任何类型的变量都不是(必须是)常数。
No. In fact, you must *not* use them:
"The argument in any instance of these macros shall be a decimal, octal, or
hexadecimal constant (as defined in 6.4.4.1) with a value that does not
exceed the limits for the corresponding type." [7.8.14, paragraph 2].
A variable of any kind is not (required to be) a constant.
2.为什么没有这样的为int_fast8_t等提供的宏以及
int_least8_t等?是否可以为其他类型的值分配
没有大小不匹配并且期望发生正确的事情?
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?
不适用通常的规则适用:您需要对可能没有的值进行强制转换合适,
确保它们合适是你的责任。 (实际规则的粗略过度简化,当然,请参阅关于隐式和明确转换的关于C的任何好书。)
S。
No. The usual rules apply: you need a cast for values that might not fit,
and it''s your responsibility to ensure they fit. (A gross oversimplification
of the actual rules, of course, see any good book on C about implicit and
explicit conversion.)
S.
>
感谢您的澄清。
Thanks for the clarification.
2006年3月25日04:30:08 -0800,santosh < sa ********* @ gmail.com>
写道:
On 25 Mar 2006 04:30:08 -0800, "santosh" <sa*********@gmail.com>
wrote:
大家好,
在stdint.h中定义了名为INT8_C,INT16_C等的转换宏,以将它们的参数转换为适当的表示形式,即
对应的类型,即int8_t,int16_t等。
我的n1124副本没有显示任何此类宏。你在哪里找到了
吗?他们为什么需要他们?您可以使用简单的
int8_t x;
定义所需
类型的变量,并且可以将表达式转换为所需类型简单
cast
x =(int8_t)< expression>
我的问题是:
1。即使从相同类型的变量分配时,是否应该使用转换宏?
2。为什么没有为int_fast8_t等提供这样的宏以及
int_least8_t等?可以为他们分配其他类型的值吗?
没有大小不匹配并期望正确的事情发生?
谢谢。
Hello all,
Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My copy of n1124 does not show any such macros. Where did you find
them? Why would they be needed? You define a variable of the desired
type with a simple
int8_t x;
and you can convert an expression to the desired type with a simple
cast
x = (int8_t)<expression>
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?
Thanks.
删除del电子邮件
Remove del for email
这篇关于stdint类型和转换......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!