Here is my code.
Код: Выделить всё
#include struct Node { int x; }; Node f(int A, int B) { if (A < 0) return {0}; return {A / B + (A % B != 0)}; } Node g(int C, int D) { if (C > 0) return {0}; return {C / D + (C % D != 0)}; } int main() { int n; scanf("%d", &n); f(1, 1); Node t = g(-1, n); printf("%d\n", t.x); } Код: Выделить всё
g++-13 tmp.cpp -o tmp -O0Код: Выделить всё
2Код: Выделить всё
1But
Код: Выделить всё
g++-13 tmp.cpp -o tmp -O2Код: Выделить всё
0I think there is no undefined behavior. Why I get an wrong result?
OS: Linux x86-64 ubuntu23.10 6.5.0-21-generic
gcc version: >=9.2.0
Источник: https://stackoverflow.com/questions/781 ... n-i-use-o2