P
pete
Ralf said:[about:]Peter said:pete <[email protected]> wrote:unsigned short port = 0;
do { [...]
} while (++port != 0);
It's undefined if INT_MAX equals USHRT_MAX.
while ((port += 1u) != 0);
That won't do what you want,
if UINT_MAX is greater than USHRT_MAX.
Would you care to explain?
Yes.
The loop (in this sub-thread) was
meant to iterate over all unsigned values.
No, it wasn't.
My comment "It's undefined if INT_MAX equals USHRT_MAX.",
refers to the increment operator causing a promotion
of type unsigned short, to type int.
(1 + INT_MAX) is undefind.
(++port) means exactly the same thing as (port = port + 1).
Peter Nilsson addressed that issue by replacing
++port with (port += 1u), thus causing the promotion to be to
type unsigned.
(1 + UINT_MAX) *is* defined as zero.
But then, as I said, the loop will do more
than just all of the values for unsigned short
if UINT_MAX is greater than USHRT_MAX.