C Puzzles

code.jpgIn this site, there is a bunch of C programming questions. Enjoy!

First puzzle:

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("%dn",array[d+1]);
      return 0;
  }

Find out what’s going wrong.

For more…

1 Response to “C Puzzles”


  1. 1 prasath September 17, 2008 at 5:35 pm

    Hi..sizeof operator return unsigned int type..so TOTAL_ELEMENTS-2 is an unsigned int constant. on comparing it with d,since d is -1 at the start,its gets implicitly typecasted to unsigned which becomes 65535<=5,in which the condition fails.. so nothing gets printed..


Leave a Reply