Let’s assume your input file contains the following as in your example:
1 + 2 + 5 - 3 =
The outer scanf reads with %d. This picks up 1 as the integer value 1 and puts it in result.
Now the inner scanf runs with %c. It reads the space after 1 so none of the if blocks are entered and the inner loop runs again. The inner scanf again reads with %c and reads the + character. This causes the if (operator == '+') block to be entered where a call to scanf uses %d. This format specifier skips over the space after the + and reads 2 as the integer value 2 and adds it to result.
The above happens again reading the space after 2, the + that follows, and the space and 5 that follows. And again for the following space, -, space, and 3.
After that, the scanf in the inner loop reads the space after 3 and does nothing, then on the next iteration it reads the = after which it prints the result and sets the flag to exit the inner loop.,
solved Addition and Subtraction in C using fscanf()