#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); }
[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); } [/code] Then I use: [code]g++-13 tmp.cpp -o tmp -O0[/code]. I input [code]2[/code], it should output [code]1[/code]. But [code]g++-13 tmp.cpp -o tmp -O2[/code] I get [code]0[/code].
I 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