c++ - Print elements in an array -
possible duplicate:
confused c macro expansion , integer arithmetic
a riddle (in c)
the output of following c program print elements in array. when run, doesn't 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 that?
this fails because sizeof returning value of type size_t, unsigned. causes comparison promote -1 unsigned, large value , make comparison fail.
you should receive warnings sign mismatch.
Comments
Post a Comment