3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C言語 unsignedの型の変数に負の数を代入するのは未定義動作ではない。

Posted at

整数変換という機能

あらゆる整数型の値は任意の他の整数型に暗黙に変換できます。

目的の型が符号なしの場合は、目的の型に収まるまで繰り返し 2^bが加算または減算されます (b は目的の型のビット数です)。 別の言い方をすると、符号なし整数はモジュロ算術を実装します。

unsigned char n = -123456; 
// => 目的の型は符号なし。 結果は 192 (つまり -123456+483*256) です。

参考

暗黙の変換 - cppreference.com

メモ

printfの実装を読んでいて「暗黙の型変換」を前提とした書き方に初めて出会ったので、メモとしてまとめました。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?