3

The following program compiles successfully but when i ran it ,it prints nothing when i initialize the for loop with -1 but when i initialize for loop with 0 it successfully traverse all the array.I want to ask that can we don't traverse the array when we initialize the for loop with negative value??

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23, 34, 12, 17, 204, 99, 16};

int main(void) {
    int d;
    //printf("%d",TOTAL_ELEMENTS);
    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++) 
    {
        printf("%d ",d);
        printf("%d\n", array[d+1]);
    }    
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
sagg1295
  • 177
  • 2
  • 11
  • 1
    [Why is (sizeof(int) > -1) false?](http://stackoverflow.com/q/34151309/995714), [Why is −1 > sizeof(int)?](http://stackoverflow.com/q/3100365/995714) – phuclv Nov 11 '16 at 07:02

4 Answers4

2

The result of sizeof operator is of type size_t, which is an unsigned type.

As a result, the type of TOTAL_ELEMENTS is also unsigned. When -1 is compared with it, it's converted to a big unsigned number. That's why d <= (TOTAL_ELEMENTS - 2) is false.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

Here d <= (TOTAL_ELEMENTS - 2) operands are subject of usual arithmetic conversions (6.3.1.8). And actually integer promotions rules act in your case:

  1. If both operands have the same type, then no further conversion is needed.

  2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

  3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

  4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

  5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

According to what you've got your code falls into clause #3, then your signed -1 is converted via rule (6.3.1.3):

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

As result it becomes a very large unsigned value, that is surely greater than TOTAL_ELEMENTS - 2 and you'll never enter the loop.

Sergio
  • 8,099
  • 2
  • 26
  • 52
1

This doesn't do what you think it does:

d <= (TOTAL_ELEMENTS - 2)

Instead, do this:

d <= int(TOTAL_ELEMENTS - 2)

Otherwise you've got a signed-vs-unsigned comparison, and your -1 becomes the largest possible size_t.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

sizeof produces a result of size_t, which is unsigned. Compare a signed and unsigned type and you can only expect things to blow up.

To elaborate, when you try to use both signed and unsigned type in arithmatic operations, the signed type will be promoted to unsigned type, producing a huge number. Thus, the value of d , promoted to unsigned type, will fail to meet the condition d <= (TOTAL_ELEMENTS - 2);, hence the loop body will not execute.

For operators that expect operands of arithmetic type cause conversions. This pattern is called the usual arithmetic conversions. for this particular case, quoting the standard, chapter §6.3.1.8

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

[...]

  • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

and, regarding the rank,

  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.

Also, for reference, quoting C11, chapter 7.19, (emphasis mine)

size_t

which is the unsigned integer type of the result of the sizeof operator;

Hint: Enable compiler warning and it will point to your mistake.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261