Print elements in an array

arrays, c, c++, printf

Solution

This fails because `sizeof` is returning a value of type `size_t`, which is unsigned. This causes the comparison to promote the `-1` to unsigned, which is generally a very large value and thus make the comparison fail.

You should receive warnings for the sign mismatch.

Problem

Possible Duplicate: Confused about C macro expansion and integer arithmetic A riddle (in C) The output of the following C program is to print the elements in the array. But when actually run, it doesn't do so. ``` #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 that?

Original source

Related problems