The program will generate factorial number until it occurs overflow. The expression r <= UINT_MAX / j
is to detect the overflow. For now, if I rewrite the expression to this r * j <= UINT_MAX
, it will enter the infinite loop. My question is NOT about how to detect the integer overflow. That is why the expression will let the process enter the infinite loop.
for (unsigned i = 0;;++i) {
unsigned r = 1;
for (unsigned j = 2; j <= i; ++j) {
if (r <= UINT_MAX / j)
r *= j;
else {
printf("Overflow!\n");
}
}
}