1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Interesting Interview Questions For C Programmer

Discussion in 'C' started by poornaMoksha, Oct 1, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    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 :

    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!!!
     
    Trinity and lionaneesh like this.
  2. lionaneesh

    lionaneesh New Member

    While Compiling the Second Problem i get a Error :-

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

    No IDEA about this! :(
     
  3. poornaMoksha

    poornaMoksha New Member

    Ok, lets discuss. So, do you know '*' operator can be applied to which kind of variables?
     
  4. lionaneesh

    lionaneesh New Member

    HmM! Its basically used to declare a pointer or as a refrence to iT! :( Maybe i am wrong!
     
  5. poornaMoksha

    poornaMoksha New Member

    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
     
  6. lionaneesh

    lionaneesh New Member

    Great Explanation! :) Thanks it really helped!
     
  7. lionaneesh

    lionaneesh New Member

    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.
     
  8. poornaMoksha

    poornaMoksha New Member

    Did you even try to execute it??
     
  9. lionaneesh

    lionaneesh New Member

    Yeah i did it executed without any problems!
     
  10. poornaMoksha

    poornaMoksha New Member

    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?
     
  11. lionaneesh

    lionaneesh New Member

    OS : Windows 7
    Compiler : Code:Blocks
    Compiled Without problem

    Code:
    0
    1
    4
    9
    16
    
    Process returned 0 (0x0)   execution time : 1.110 s
    Press any key to continue.
    
     
  12. poornaMoksha

    poornaMoksha New Member

    Can you give it a try on Unix/Linux ??
     
  13. lionaneesh

    lionaneesh New Member

    Yeah Sure , I will! But can you explain me the reason why its giving error on Unix and not on Windows
     
  14. poornaMoksha

    poornaMoksha New Member

    I dunno why its not giving any error on windows but logically when we are about to call free(), we have incremented the address held at pointer 'p' (as we have used p++ many times in loop). Now when we do a free, then 'p' contains a different address than what malloc() gave it. So ideally the program should crash at free().
     
  15. lionaneesh

    lionaneesh New Member

    I know that and yeah regarding the windows issue , I think my compiler provides some support for preventing memory leak ! Thats why no errors! :D
     
  16. lionaneesh

    lionaneesh New Member

    Can u please explain my other problems too! :)
     
  17. poornaMoksha

    poornaMoksha New Member

    For problem '3', google how or rather in which order the function arguments are processed/passed when a function call is made.
     
  18. hanleyhansen

    hanleyhansen New Member

    This is good stuff. Are you going to provide the answer to all the questions eventually?
     
  19. poornaMoksha

    poornaMoksha New Member

    Thanks, I thought of having interactive sessions with different users on this article. If somebody comes up with some doubt/query then I'll definitely answer that but providing answers directly would defeat the purpose as of now. All I want is that anyone who finds these questions worth trying, should try them. Answers, doubts, queries can be discussed here.
     
  20. It's a very interesting article.I like C program very much.So it's very helpfull to me.I can understand more programs&it's answer.
     

Share This Page