Q:

What am I missing in the following program?

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

int main() {
    int d;

    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
        printf("%d\n", array[d + 1]);

    return 0;
}

Why is the for loop not run even once?

A:

The issue is that sizeof() returns size_t which is unsigned.  Comparison of -1 with TOTAL_ELEMENTS - 2 should result in a warning indicating you have compared unsigned with signed.  When this comparison happens, the -1 is converted to an unsigned value which is MAX_UINT.  On a 32-bit platform, both -1 and MAX_UINT are 0xFFFFFFFF.
Your TOTAL_ELEMENTS() macro could incorporate a cast to (int) but that isn't technically correct because size_t has a larger value range than int.  Best to change your loop variable so that it is declare as size_t and never becomes negative.

A:

You're mixing signed and unsigned arithmetic. sizeof yields a size_t (an unsigned type). When you do the d <= (TOTAL_ELEMENTS -2) the d gets converted to unsigned, and then compared. Since -1 becomes the largest value in the target type when converted to unsigned, your condition becomes something like 0xffffffff <= 5, which is always false so the loop never executes.

