Someone spoke to me about wraparound in C (0xffff + 0x0001 = 0x0000), and this led me to the following situation:
int main() { unsigned int a; for (a = 0; a > -1; a++) printf("%d\n", a); return 0; }
Compiling with GCC, this program crashes without starting the loop, which I assume because it -1was implicitly added to 0xffff. The same thing happens when you switch intto long. However, when switching to a charprogram, it works endlessly. I would expect that since I intdid not complete the cycle, none of them would char. Can someone explain what implicit casting the compiler performs in this situation, and is it defined in one of the releases of the C standard or is it dependent on the compiler?
-1
0xffff
int
long
char
ISO/IEC 9899:
, .
.
, , . , . , , . , , , , . :
-, (...)
, double (...)
, float (...)
. :
, unsigned (...)
, , .
C :
unsigned int a; /* ... */ a > -1
>, unsigned int, int. C : unsigned int, > :
>
unsigned int
a > (unsigned int) -1
-1 unsigned int unsigned int, a - 0, false (0).
a
0
, a char int, -1 unsigned int, 0 > -1 (1), .
0 > -1
1
, C, , , , .
, , int int32, :
uint32_t > int32_t
"uint32_t" . "uint32_t".
:
uint8_t > int32_t
"int32_t" .
, , "int", .
, :
(int)unsigned char > int(-1)