spamsink: (lenin)
[personal profile] spamsink


В какой-то мере навеяно постом [livejournal.com profile] ivan_gandhi

Что произойдет в вашей системе и что, по-вашему, должно произойти при попытке компиляции (и, если компиляция была/должна быть успешной, то запуска) следующей программы на Си:

#include <stdio.h>
main() {
printf("%d %d %d\n", (int)(-1.0/0.0), (int)(0.0/0.0), (int)(1.0/0.0));
}

Date: 2013-11-18 09:22 am (UTC)
From: [identity profile] archaicos.livejournal.com
Если скомпилируется, то UB.

Date: 2013-11-19 06:14 am (UTC)
From: [identity profile] archaicos.livejournal.com
По определению?

C99:

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

Date: 2013-11-19 06:50 am (UTC)
From: [identity profile] archaicos.livejournal.com
Получил бы ты двойку за такой ответ на экзамене по Си. :)

Date: 2013-11-19 06:56 am (UTC)
From: [identity profile] archaicos.livejournal.com
А кто умеет, мучается с UB в своём коде. :)

Date: 2013-11-19 07:26 am (UTC)
From: [identity profile] archaicos.livejournal.com
В Верилоге, поди, тоже есть «тонкости» для мучений.

Date: 2013-11-19 07:33 am (UTC)
From: [identity profile] archaicos.livejournal.com
Чудно.

Date: 2013-11-19 06:52 am (UTC)
From: [identity profile] archaicos.livejournal.com
Ага, известное «It works on my computer». :)

А потом обновляешь компилятор или запускаешь другой компилятор на том же процессоре с той же ОС, и результат другой.
Page generated Mar. 5th, 2026 05:52 am
Powered by Dreamwidth Studios