さて、符号なし整数の減算は動作を定義しました、またそれはトリッキーなことです。2つの符号なし整数を減算する場合、result(lvalue)型が明示的に指定されていない場合、resultは上位型intにプロモートされます。後者の場合、たとえば、int8_t result = a --b; (aとbがint8_tタイプの場合)非常に奇妙な動作をする可能性があります。つまり、推移性のプロパティが失われる可能性があります(つまり、a>bおよびb>cの場合、a> cであることは事実です)。推移性が失われると、ツリータイプのデータ構造の作業が破壊される可能性があります。ソート、検索、符号なし整数減算を使用してどちらのキーが高いか低いかを推測するツリー構築のための比較機能を提供しないように注意する必要があります。
以下の例を参照してください。
#include <stdint.h>
#include <stdio.h>
void main()
{
uint8_t a = 255;
uint8_t b = 100;
uint8_t c = 150;
printf("uint8_t a = %+d, b = %+d, c = %+d\n\n", a, b, c);
printf(" b - a = %+d\tpromotion to int type\n"
" (int8_t)(b - a) = %+d\n\n"
" b + a = %+d\tpromotion to int type\n"
"(uint8_t)(b + a) = %+d\tmodular arithmetic\n"
" b + a %% %d = %+d\n\n",
b - a, (int8_t)(b - a),
b + a, (uint8_t)(b + a),
UINT8_MAX + 1,
(b + a) % (UINT8_MAX + 1));
printf("c %s b (b - c = %d), b %s a (b - a = %d), AND c %s a (c - a = %d)\n",
(int8_t)(c - b) < 0 ? "<" : ">", (int8_t)(c - b),
(int8_t)(b - a) < 0 ? "<" : ">", (int8_t)(b - a),
(int8_t)(c - a) < 0 ? "<" : ">", (int8_t)(c - a));
}
$ ./a.out
uint8_t a = +255, b = +100, c = +150
b - a = -155 promotion to int type
(int8_t)(b - a) = +101
b + a = +355 promotion to int type
(uint8_t)(b + a) = +99 modular arithmetic
b + a % 256 = +99
c > b (b - c = 50), b > a (b - a = 101), AND c < a (c - a = -105)