C interview Questions

struct aaa
{
     struct aaa *prev;
     int i;
     struct aaa *next;
};
main()
{
     struct aaa abc,def,ghi,jkl;
     int x=100;
     abc.i=0;abc.prev=&jkl;
     abc.next=&def;
     def.i=1;def.prev=&abc;def.next=&ghi;
     ghi.i=2;ghi.prev=&def;
     ghi.next=&jkl;
     jkl.i=3;jkl.prev=&ghi;jkl.next=&abc;
     x=abc.next->next->prev->next->i;
     printf("%d",x);
}

Answer:
     2
Explanation:
     above all statements form a double circular linked list;
     abc.next->next->prev->next->i
     this one points to "ghi" node the value of at particular node is 2.

struct point
{
     int x;
     int y;
};
struct point origin,*pp;
main()
{
     pp=&origin;
     printf("origin is(%d%d)\n",(*pp).x,(*pp).y);
     printf("origin is (%d%d)\n",pp->x,pp->y);
}

Answer:
     origin is(0,0)
     origin is(0,0)
Explanation:
     pp is a pointer to structure. we can access the elements of the structure either with arrow mark or with indirection operator.
Note:
     Since structure point is globally declared x & y are initialized as zeroes

main()
{
     int i=_l_abc(10);
     printf("%d\n",--i);
}
int _l_abc(int i)
{
     return(i++);
}

Answer:
     9
Explanation:
     return(i++) it will first return i and then increments. i.e. 10 will be returned.

main()
{
     char *p;
     int *q;
     long *r;
     p=q=r=0;
     p++;
     q++;
     r++;
     printf("%p...%p...%p",p,q,r);
}

Answer:
     0001...0002...0004
Explanation:
     ++ operator when applied to pointers increments address according to their corresponding data-types.

main()
{
     char c=' ',x,convert(z);
     getc(c);
     if((c>='a') && (c<='z'))
          x=convert(c);
          printf("%c",x);
}
convert(z)
{
     return z-32;
}

Answer:
     Compiler error
Explanation:
     declaration of convert and format of getc() are wrong.

main(int argc, char **argv)
{
      printf("enter the character");
      getchar();
      sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
      return num1+num2;
}

Answer:
     Compiler error.
Explanation:
     argv[1] & argv[2] are strings. They are passed to the function sum without converting it to integer values.

# include
int one_d[]={1,2,3};
main()
{
      int *ptr;
      ptr=one_d;
      ptr+=3;
      printf("%d",*ptr);
}

Answer:
     garbage value
Explanation:
     ptr pointer is pointing to out of the array range of one_d.

# include
aaa()
{
     printf("hi");
}
bbb()
{
     printf("hello");
}
ccc()
{
     printf("bye");
}
main()
{
     int (*ptr[3])();
     ptr[0]=aaa;
     ptr[1]=bbb;
     ptr[2]=ccc;
     ptr[2]();
}

Answer:
     bye
Explanation:
     ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc.

#include
main()
{
       FILE *ptr;
       char i;
       ptr=fopen("zzz.c","r");
       while((i=fgetch(ptr))!=EOF)
       printf("%c",i);
}

Answer:
     contents of zzz.c followed by an infinite loop
Explanation:
     The condition is checked against EOF, it should be checked against NULL.

main()
{
       int i =0;j=0;
      if(i && j++)
            printf("%d..%d",i++,j);
            printf("%d..%d,i,j);
}

Answer:
     0..0
Explanation:
     The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed.

Google