The expected 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; }Find out what's going wrong.
Can you please explain the solution and cause of this problem..
ReplyDeleteto understand properly run gcc -E
ReplyDeletegcc -E test1.c
Deleteneed to typecast value to signed int as below
Deleteneed to typecast as for(d=-1;d <= (int) (TOTAL_ELEMENTS-2);d++)
Runs perfectly in turbo c++
ReplyDeleted is signed in intiailisation part but in codition it is unsigned
ReplyDeletethat's the problem
Here the for loop condition begin with -1 that is wrong put d=0 then the elts will be printed...
ReplyDelete