'Stacked in a while loop
I have this simple problem:
# include <stdio.h>
int main(){
unsigned a, b, x, y, temp;
a=100;
b=30;
x=a*b;
printf("%u", x);
while(b) temp=a%b, a=b, b=temp; // try to comment this line
y=b;
x/=y;
printf("%u", x);
return 0;
}
If I put this code into main.c it returns "Program finished with exit code 0", and there will be no printf, else if I comment the while loop I will get the printfs.
Can you explain why the while loop influences the program this way?
What actually I should achieve with this problem is the division by 0 at the final line x/=y;.
Solution 1:[1]
Put fflush(stdout); after each printf() call.
When the division by 0 is occured, the program will abort without flushing contents in output buffer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Itagaki Fumihiko |
