#c
Вопрос короткий.
unsigned char c = 1, d = 2;
printf("%d %d %d",sizeof(c),sizeof(d),sizeof(c&d)); // выводит 1 1 4
Скажите, почему при конъюнкции получившееся выражение становится типа int? В стандарте
C99 четко сказано, что:
The integer promotions are applied only: as part of the usual arithmetic conversions,
to certain
argument expressions, to the operands of the unary +, -, and ~ operators, and to
both operands of the
shift operators, as specified by their respective subclauses.
В чем причина? Буду благодарен за ответ.
Ответы
Ответ 1
Так это и есть действие the usual arithmetic conversions, о которых говорится в вашей цитате. Спецификация бинарного & ясно говорит 6.5.10 Bitwise AND operator 3 The usual arithmetic conversions are performed on the operands. Для целочисленных типов the usual arithmetic conversions начинаются с integer promotions. В вашем случае сработали именно integer promotions и тип unsigned char был превращен в тип int.
Комментариев нет:
Отправить комментарий