Test Your C Skills


                     


 1) Shift the bits as illustrated above In the picture

     unsigned char A = 171;
     unsigned char B = 223;
     unsigned char C = 0;
     //Solution
     C = (A >> 3) | ((B >> 5) << 5);
     printf("A = %d , B = %d , C = %d ",A,B,C);


2) Find out the two logical Errors in the following code...

       
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  //Find out two logical Errors in the code
  typedef struct dbTable
  {
    char *key;
    int value;
  } dbTable;
  /* prototypes to get data from Hash table */
  int getData(dbTable *pdbTable , char *key);
  int getSize(dbTable *pdbTable);
  int main()
  {
    dbTable dbTableHash[] = {{"test1",1},{"test2",2},{"test3",3}, 
    {"test4",4},      {"test5",5},{"test6",6}};
    int value = getData(dbTableHash,"test6");
    printf("value = %d \n",value);
  }
  int getData(dbTable *pdbTable , char *key)
  {
    int i = 0;
    for(i = 0 ; i < sizeof(pdbTable) ; i++)
    {
        if(strcmp(pdbTable[i].key , key) == 0)
            break
    }
  
    return pdbTable[i].value;
  }
         
    

No comments: