sizeof returns an unsigned integer, so TOTAL_ELEMENTS also unsigned.
d . Initially, d is -1 . However, when comparing, d implicitly displayed unsigned, so when comparing with TOTAL_ELEMENTS it is no longer -1 , it is actually UINT_MAX (which is 4294967295 on my machine, but may differ for others).
Besides,
If you want to fix this, enter TOTAL_ELEMENTS in int :
for(d = -1; d <= (int)(TOTAL_ELEMENTS - 2); d++)
This will print:
23 34 12 17 204 99 16
As expected. You can also see the comparison operation for unsigned integers and sign for more information on the topic of comparisons without signatures.
It's worth noting that turning on compiler warnings would help you figure out what's going on (as hyde noted in comment ):
$ gcc -Wall -Wextra test.c test.c:7:17: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare] for(d = 0; d < TOTAL_ELEMENTS; d++) ~ ^ ~~~~~~~~~~~~~~ 1 warning generated.
Alternatively, why not start d at 0 and instead switch to TOTAL_ELEMENTS - 1 ? You can even refuse to typecast, which is necessary only for the angular case d = -1 .
for(d = 0; d < TOTAL_ELEMENTS; d++) printf("%d\n", array[d]);
The relevant standard C99 passages are given as footnotes:
6.3.1.8p2 defines a conversion from an unsigned signed type.
If an operand that has an unsigned integer type has a rank greater than or equal to the ranks of the type of another operand, then the operand with a signed integer type is converted to the operand type with an unsigned integer.
6.3.1.3p2 determines how the conversion is performed: by adding UINT_MAX + 1 to the signed view.
If the new type is unsigned, the value is converted by repeatedly adding or subtracting more than one maximum value that can be represented in the new type until the value is in the range of the new type.
So -1 => -1 + (UINT_MAX + 1) = UINT_MAX , for this scenario.
cᴏʟᴅsᴘᴇᴇᴅ Aug 13 '17 at 5:58 on 2017-08-13 05:58
source share