2

複数の異なる int 型を使用するプログラムがあります。

最もよく使用されるuint64_tのは と 標準intです。しかし、それらが混在する操作を安全に実行できるかどうかは疑問です。

たとえば、 があり、それに を追加して、その値を別の として保存しuint64_tたいとします。intuint64_t

そのようなことをすることは安全ですか?操作を使用する前にinttoをキャストする必要がありますか?uint64_t

私は本当にそれについての情報をオンラインで見つけることができません. それは許可されているだけかもしれませんし、誰もそれについて質問したり、私の Google クエリが間違っていたりすることはありません。

とにかく、基本的に私の質問は、さまざまなタイプの int を組み合わせて操作を行うことができるかということです。

4

2 に答える 2

4

はい、できます。

コンパイラが変換を処理します。入力よりも小さいコンテナーに結果を格納する場合、心配する必要があるのはオーバーフローだけです。

必要な Google 検索用語は「暗黙の型変換」です。たとえばhttp://pic.dhe.ibm.com/infocenter/ratdevz/v8r5/index.jsp?topic=%2Fcom.ibm.tpf.toolkit.compilersを参照.doc%2Fref%2Flangref_os390%2Fcbclr21011.htm

そのリンクには次の表が含まれています。

算術変換は次の順序で行われます。

Operand Type                                  Conversion
---------------------------------------------+--------------------------------------------
One operand has long double type             | The other operand is converted to long double type.
---------------------------------------------+--------------------------------------------
One operand has double type                  | The other operand is converted to double.
---------------------------------------------+--------------------------------------------
One operand has float type                   | The other operand is converted to float.
---------------------------------------------+--------------------------------------------
One operand has unsigned long long int type  | The other operand is converted to unsigned long long int.
---------------------------------------------+--------------------------------------------
One operand has long long int type           | The other operand is converted to long long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned long int type       | The other operand is converted to unsigned long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            |
and the other operand has long int type      |
and the value of the unsigned int can be     |
 represented in a long int                   | The operand with unsigned int type is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            |
and the other operand has long int type      |
and the value of the unsigned int cannot be  |
represented in a long int                    | Both operands are converted to unsigned long int
---------------------------------------------+--------------------------------------------
One operand has long int type                | The other operand is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            | The other operand is converted to unsigned int.
---------------------------------------------+--------------------------------------------
Both operands have int type                  | The result is type int.
---------------------------------------------+--------------------------------------------
于 2014-03-12T17:13:53.797 に答える
0

C標準は言う、

符号なし整数型のオペランドのランクが他のオペランドの型のランク以上である場合、符号付き整数型のオペランドは符号なし整数型のオペランドの型に変換されます。

したがって、int&unsigned intは同じランクであるため、それらを追加することができ、それらを追加すると、結果が再び to に残るようintに変換されます。unsigned intunsigned int

于 2014-03-12T17:18:05.783 に答える