This is because of the output buffer.
The buffer hasn't been emptied yet, so it looks like scanf came out before the output.
fflush(stdout);
Force the buffer to empty using fflush before scanf.
I am attaching a link for your reference.
C language coding painting: utilizing input/output buffers
© 2024 OneMinuteCode. All rights reserved.