Tuesday, April 13, 2010

Solution of C puzzeles

1 #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;
  }
------------------------------------------------------------------------
1.Solution :   Problem is in comparison of d = -1 as unsigned int to Macro which is 5
4294967295 (%u of -1 ) > 5(%u of TOTAL_ELEMENTS-2)
---------------------------------------------------------------------------------------------------------------------------------
2
OS_HP-UX_print is Erroneous '-'is not allowed in names .
-------------------------------------------------------------------
3 
  #include <stdio.h>
  #include <unistd.h>
  int main()
  {
          while(1)
          {
                  fprintf(stdout,"hello-out");
                  fprintf(stderr,"hello-err");
                  sleep(1);
          }
          return 0;
  }
------------------------
Output is hello-errhello-err.......
Reason is stdout is buffered  to print we need fflush(stdout)  or wait till buffer fills .
stderr is not buffered so hello-err cones to screen instantaneously
-------------------------------------------------------------------------- 
3 #include 
  void foobar1(void)
  {
   printf("In foobar1\n");
  }

  void foobar2()
  {
   printf("In foobar2\n");
  }

  int main()
  {
     char ch = 'a';
     foobar1();
     foobar2(33, ch);
     return 0;
  }
OUTPUT : In foobar1
         In foobar2  catch : Foorbar2 will be called irrespective of No and type of 
Arguments   As declaration of foobar2 specifies nothing
Program 2:
  #include 
  void foobar1(void)
  {
   printf("In foobar1\n");
  }

  void foobar2()
  {
   printf("In foobar2\n");
  }

  int main()
  {
     char ch = 'a';
     foobar1(33, ch);
     foobar2();
     return 0;
  } 
OUTPUT: Not compile error: too many arguments to function `foobar1'
catch : fooobar1 declaration clearly says NO argument(void ) we cant pass anything 
--------------------------------------------------------------------------------------
4 #include 
  int main()
  {
      int i;
      i = 10;
      printf("i : %d\n",i);
      printf("sizeof(i++) is: %d\n",sizeof(i++));
      printf("i : %d\n",i);
      return 0;
  } 
OUTPUT:
i : 10
sizeof(i++) is: 4
i : 10
CATCH : sizeof is a compile-time operator, which means that during  
compilation, sizeof and its operand get replaced by the result-value. 
i++ will never wet executed 
-------------------------------------------------------------------------
int a = 1,2;  ---> not compile 2 will be taken as variable which is not allowed
 int a = (8,9,10); -->Allowed initialize a as 10 
int i;
i = 1,2,3;
i will be assigned as 1 
------------------------------------------------------------------------
5 #include 
  #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
  int FiveTimes(int a)
  {
      int t;
      t = a<<2 + a;
      return t;
  }

  int main()
  {
      int a = 1, b = 2,c = 3;
      PrintInt(FiveTimes(a));
      PrintInt(FiveTimes(b));
      PrintInt(FiveTimes(c));
      return 0;
  }
OUTPUT :
FiveTimes(a) : 8
FiveTimes(b) : 32
FiveTimes(c) : 96
strange ?? 
change   t = (a<<2) + a; Output will be fine due to precedence a is first added to 2 
and then a is shifted .
---------------------------------------------------------------------------