Interesting Interview Questions For C Programmer

poornaMoksha's Avatar author of Interesting Interview Questions For C Programmer
This is an article on Interesting Interview Questions For C Programmer in C.
Rated 5.00 By 1 users
In this article, I'll present some interesting 'C' problems for which I will not provide the answer. If you are not bale to solve any problem, then just leave a comment here so that everybody is benefited when the problem is discussed.

Problems



1) Will the program execute without any problem?
Code:
#include <stdlib.h>
  #include <stdio.h>
  #define SIZE 5 
  int main()
  {
      int *p, i;

      p = malloc(SIZE*sizeof(int));

      for (i=0; i<SIZE; i++)
          *(p + i) = i * i;
      for (i=0; i<SIZE; i++)
          printf("%d\n", *p++);
      free(p);
      return 0;
  }
2) Is there any problem with this piece?
Code:
#include <stdio.h>
  int main()
  {
      int* ptr1,ptr2;
      ptr1 = malloc(sizeof(int));
      ptr2 = ptr1;
      *ptr2 = 10;
      return 0;
  }
3) Here in this code, the result comes out to be '0', why?
Code:
#include<stdio.h>

int func(int a, int b, int *res)
{
   *res = b/a;
    return 0; // Sum successful
}

int main(void)
{
    int a=2,b=6,res;

    printf("\n Func returned = [%d], res = [%d]\n",func(a,b,&res), res);

    return 0;
}
4)This program gives 'seg-fault' when executed, can you tell the reason why ?
Code:
#include <stdio.h>
  int main()
  {
      int n;
      printf("Enter a number:\n");
      scanf("%d\n",n);

      printf("You entered %d \n",n);
      return 0;
  }
5)Here is a visual treat. The output comes out to be 100 but don't you think that it should be '10' ?
Code:
#include <stdio.h>
#include<stdlib.h>

  #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
  int main()
  {
      int y = 100;
      int *p;
      p = malloc(sizeof(int));
      *p = 10;
      y = y/*p; /*dividing y by *p */;
      PrintInt(y);
      return 0;
  }
6)Somebody was saying that I am in real trouble if any one comes to know this logic behind my binary. Can you tell the reason?
Code:
#include <stdio.h>
  int main()
  {
      char str[80];
      printf("Enter the string:");
      scanf("%s",str);
      printf("You entered:%s\n",str);

      return 0;
  }
7)Shouldn't the output be 40 here ?
Code:
#define SIZE 10
  void size(int arr[SIZE])
  {
          printf("size of array is:%d\n",sizeof(arr));
  }

  int main()
  {
          int arr[SIZE];
          size(arr);
          return 0;
  }
8) I want to change the entry point of my C program. ie I want my function 'func()' to be the first function to run rather than the traditional main() function. Is this possible?

9)I see the output of the following code may vary from machine to machine, whats happening?
Code:
#include<stdio.h>

int main(void)
{
    int *ptr1 = (int*)400;
    int *ptr2 = (int*)200;

    printf("\n Diff is [%ld]\n",(ptr1 - ptr2));

    return 0;
}
10)I am expecting the output to be ONE but I am getting NONE. why?
Code:
#include<stdio.h>
  int main()
  {
          int a=1;
          switch(a)
          {
                  case '1':
                      printf("ONE\n");
                      break;
                  case '2':
                      printf("TWO\n");
                      break;
                  default:
                      printf("NONE\n");
          }
          return 0;
  }
11)This program is seg-faulting when I am trying to execute it, what could be the reason?
Code:
#include<stdio.h>
  int main()
  {
     char str[] = "hello";
     char *st = "bye";

     str[0] = 'b';
     st[0] = 'h';

     return 0;
  }
12)Why is the value printed by the two printf's different?
Code:
#include<stdio.h>

int* func()
{
   static int count;
   count ++;

   if(count == 2)
   {
       return NULL;
   }
   int a = 10, b=20, c = 0;

   c = b/a;

   return &c;
}
int main()
{
   int *ret1 = func();
   printf("\n ret = [%d]\n",*ret1);

   int *ret2 = func();
   if(ret2 == NULL)
   {
       printf("\n This time function returned NULL\n");
   }

   printf("\n ret = [%d]\n",*ret1);
   return 0;
}
13) In the following program I get some weird output when I print str. why?
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
  {
     char str[] = {'h','e','l','l','o'};
     char *st = "bye";

     printf("\n st = [%s], str = [%s]\n",st,str);
     return 0;
  }
14)What should be the output of the following code and why?
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
  {
     int *st = "bye all";
     st++;
     printf("\n *st = [%c]\n", *((char*)st));
     return 0;
  }
15)What should be the order of print statements here ?
Code:
#include<stdio.h>
#include<stdlib.h>

int func1(void)
{
    printf("\nInside func1\n");
    return 0;
}

int func2(void)
{
    printf("\nInside func2\n");
    return 0;
}

int main()
{
    printf("\n Inside main\n");
    printf("func1 returned [%d], func 2 returned[%d]",func1(), func2());
    return 0;
}
16)Why does the following piece of code seg-fault after sometime?
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
   printf("\n Inside main\n");
   main();
   return 0;
}
17) Shouldn't the size of array here be 10? Why is it coming out to be 40?
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
   int arr[10];
   printf("\n sizeof array arr = [%lu]\n",sizeof(arr));
   return 0;
}
18)Why does this for loop execute only once, shouldn't it execute 10 times?
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
   int i =0;

   for(i = 0; i<10; i++);
       printf("\n i  = [%d]\n",i);

   return 0;
}
19)Some times in certain scenarios when you try to run a program, you get an error like "Operation not permitted", do you know why this error is thrown in most of the cases?Like the other day I was using pcap library functions to sniff network traffic but when I ran my program the first error that I got was :

Quote:
pcap_open_live(): socket: Operation not permitted
Do you know why these kind of problems occur?

20)You would have observed an error when you try to declare same variable twice in a function but can there be a way to make this happen??

Conclusion



Here were some interesting questions on basic 'C' fundamentals. I'd love to discuss here if anyone has some doubt in any of the problem.

Stay tuned for more!!!
lionaneesh, Trinity likes this
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
While Compiling the Second Problem i get a Error :-

prob.c|7|error: invalid type argument of 'unary *' (have 'int')|

No IDEA about this!
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Ok, lets discuss. So, do you know '*' operator can be applied to which kind of variables?
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by poornaMoksha View Post
Ok, lets discuss. So, do you know '*' operator can be applied to which kind of variables?
HmM! Its basically used to declare a pointer or as a refrence to iT! Maybe i am wrong!
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Its basically used as a dereference or a value-of operator. It is mostly applied to pointers to extract the value at the address held by pointer. Here if you see carefully, you will find that ptr2 is not a pointer. it is declared as an integer.

The catch here is that if you want to declare more than one pointers of same type in a line, then * operator has to be applied with each pointer name rather than with type.

For example, if ptr1 and ptr2 are to be declared as integer pointers then declare them like :

Code:
 int *ptr1, *ptr2; // Correct way
int *ptr1, ptr2; // wrong, as ptr2 here is merely an int
So one should bring in practice of associating * operator with pointer name.

Quote:
int *ptr;
rather than

Quote:
int* ptr;

Hope it helps
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by poornaMoksha View Post
Its basically used as a dereference or a value-of operator. It is mostly applied to pointers to extract the value at the address held by pointer. Here if you see carefully, you will find that ptr2 is not a pointer. it is declared as an integer.

The catch here is that if you want to declare more than one pointers of same type in a line, then * operator has to be applied with each pointer name rather than with type.

For example, if ptr1 and ptr2 are to be declared as integer pointers then declare them like :

Code:
 int *ptr1, *ptr2; // Correct way
int *ptr1, ptr2; // wrong, as ptr2 here is merely an int
So one should bring in practice of associating * operator with pointer name.



rather than




Hope it helps
Great Explanation! Thanks it really helped!
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Here is My Attempt to First ten Questions :-

1. Yeah Why NOt!
2,3 No Idea About This ?? :-?
4. U dint gave the address of the int variable in scanf() , corrected code should be
Code:
scanf("%d\n",&n);
5. Got me Searching for My C Reference !! No idea! Help Please.
6. Buffer Overflow , Yeah he's definitely in trouble!
7. I think i am right with this , Point me if i am wrong :-

Arrays when provided as a argument to a function are actually treated as pointers , So what we are exactly doing here is that providing a pointer to very first variable in the array which is a random integer , therefore the size is ouput as 4

8. Yeah we can! I met with this problem when i was reverse engineering a Crackme!

You can change the entry point function!
Like in th case of GCC :-

You can use the -e flag

Code:
gcc -e myfunc prog.c -o prog.c
9. HmM! I see there is something related to size of the int! But cant get the real point of it ! Please explain!

10. You are actually comparing it to char , i mean you added the '1' instead of 1.
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Quote:
Originally Posted by lionaneesh View Post
1. Yeah Why NOt!
Did you even try to execute it??
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by poornaMoksha View Post
Did you even try to execute it??
Yeah i did it executed without any problems!
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Well, I am getting following output :
Code:
0
1
4
9
16
*** glibc detected *** ./1: free(): invalid pointer: 0x00000000021ee024 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4aa73285b6]
/lib/libc.so.6(cfree+0x73)[0x7f4aa732ee83]
./1[0x400631]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4aa72cfc4d]
./1[0x4004f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 1442049                            /home/himanshu/practice/1
00600000-00601000 r--p 00000000 08:05 1442049                            /home/himanshu/practice/1
00601000-00602000 rw-p 00001000 08:05 1442049                            /home/himanshu/practice/1
021ee000-0220f000 rw-p 00000000 00:00 0                                  [heap]
7f4aa0000000-7f4aa0021000 rw-p 00000000 00:00 0 
7f4aa0021000-7f4aa4000000 ---p 00000000 00:00 0 
7f4aa709a000-7f4aa70b0000 r-xp 00000000 08:05 262224                     /lib/libgcc_s.so.1
7f4aa70b0000-7f4aa72af000 ---p 00016000 08:05 262224                     /lib/libgcc_s.so.1
7f4aa72af000-7f4aa72b0000 r--p 00015000 08:05 262224                     /lib/libgcc_s.so.1
7f4aa72b0000-7f4aa72b1000 rw-p 00016000 08:05 262224                     /lib/libgcc_s.so.1
7f4aa72b1000-7f4aa742b000 r-xp 00000000 08:05 265935                     /lib/libc-2.11.1.so
7f4aa742b000-7f4aa762a000 ---p 0017a000 08:05 265935                     /lib/libc-2.11.1.so
7f4aa762a000-7f4aa762e000 r--p 00179000 08:05 265935                     /lib/libc-2.11.1.so
7f4aa762e000-7f4aa762f000 rw-p 0017d000 08:05 265935                     /lib/libc-2.11.1.so
7f4aa762f000-7f4aa7634000 rw-p 00000000 00:00 0 
7f4aa7634000-7f4aa7654000 r-xp 00000000 08:05 265932                     /lib/ld-2.11.1.so
7f4aa7831000-7f4aa7834000 rw-p 00000000 00:00 0 
7f4aa7850000-7f4aa7853000 rw-p 00000000 00:00 0 
7f4aa7853000-7f4aa7854000 r--p 0001f000 08:05 265932                     /lib/ld-2.11.1.so
7f4aa7854000-7f4aa7855000 rw-p 00020000 08:05 265932                     /lib/ld-2.11.1.so
7f4aa7855000-7f4aa7856000 rw-p 00000000 00:00 0 
7fff4a6a9000-7fff4a6be000 rw-p 00000000 00:00 0                          [stack]
7fff4a7c2000-7fff4a7c3000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted
I dunno how its working on your system?