#includeFind out what's going wrong.
#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;
}
Solution:
Sizeof Operator's return type is unsigned. When signed variable is compared with unsigned variable/value, the signed variable is promoted to unsigned.
So in the above case d=-1 and which be be promoted to unsigned value which become huge compared to (TOTAL_ELEMENTS-2) hence the condition will result in false and the loop won't be executed.
SO nothing gets printed.
No comments:
Post a Comment