C interview questions

Frequently Asked Questions – C

  1. What does static variable mean? Ans: static declaration limits the variable to file or function scope,Even though its existence is through life time of process only the file or function can modify the variable
  2. What is a pointer? Ans : pointer is address to a location pointers can store address of predefined data types or structures (derived data types) or functions.
  3. What is a structure? Ans : Structure is a derived data type which can be group of similar data types or different data types
  4. What are the differences between structures and arrays? Ans :Arrays is a group of similar data types but Structures can be group of different data types
  5. In header files whether functions are declared or defined? Ans : Generally declared.
  6. What are the differences between malloc() and realloc() and calloc()? Ans :malloc allocates required amount of memory where as realloc allocates required amount of memory but with values retained and calloc allocates memory with requested amount of memory and sets all values to zeoros.                Prototype :  void * malloc (size)   void * realloc(old ptr,size) void*calloc(size_t noofelements,size_t size of each element)
  7. What are macros? what are its advantages and disadvantages? Ans:.Macros are processor directive which will be replaced at compile time
    the disadvantage with macros is that they just replace the code they are not function calls. similarly the advantage is they can reduce time for replacing the same values.
  8. Difference between pass by reference and pass by value? Ans : Pass by value just passes the value from caller to calling function so the called function cannot modify the values in caller function.But Pass by reference will pass the address to the caller function instead of value if called function requires to modify any value it can directly modify.
  9. What is static identifier? Ans :
  10. Where are the auto variables stored? Ans : Auto variable are stored in stack
  11. Where does global, static, local, register variables, free memory and C Program instructions get stored? Ans : local – stack, free memory – heap,c Program instructions – Text or code segment,register variables – registers,static – bss, global – Data segment refer the following document – segment is C
  12. Difference between arrays and linked list? Ans: Arrays are created at statically but linked list can be crated dynamically. Arrays will be in stack but linked list are  heap.
  13. What are enumerations? Ans:In mathematics and theoretical computer science, the broadest and most abstract definition of an enumeration of a set is an exact listing of all of its elements (perhaps with repetition).
  14. Describe about storage allocation and scope of global, extern, static, local and register variables? Ans : global – visible to all files can be used across files by using keyword extern, scope is through out program – location Data segment. static – similar to global variable(only regarding lifetime of the variable) but scope limited to function or file – location BSS .local – scope with in the function located in stack.register – scope with in the function – register.
  15. What are register variables? What are the advantage of using register variables? Ans :If any variable is declared as register is called register variable. Advantage is fast access.
  16. What is the use of typedef? Ans : typedef – The intent is to make it easier for programmers to comprehend the source code
  17. Can we specify variable field width in a scanf() format string? If possible how? Ans : goto wikianswers
  18. Out of fgets() and gets() which function is safe to use and why?  Ans : goto wikianswers
  19. Difference between strdup and strcpy? Ans : goto wikianswer
  20. What is recursion? Ans : recursion means calling the same function it self.
  21. Differentiate between a for loop and a while loop? What are it uses? Ans : for loop and while loop are similar but in for loop you can initialize the value which will be done only at entry to for loop and you can do other functions like increment or decrement(we often do) or printing etc.
  22. What are the different storage classes in C? Ans : auto,register,static,extern
  23. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? Ans : p = a, &a[i][j][k][l] is p+i+j+k+l;
  24. What is difference between Structure and Unions? Ans : in case of structure each variables have their own storage location whereas for unions all the variables share a common memory location. In structure all the variables can be handled at a time but for union only a member can be handled at a time.
  25. What the advantages of using Unions? Ans : unions share common memory location
  26. What are the advantages of using pointers in a program? Ans : refer wikianswers
  27. What is the difference between Strings and Arrays? Ans : refer wikianswers
  28. In a header file whether functions are declared or defined? Ans : declared usually.
  29. What is a far pointer? where we use it? Ans : refer wikianswers
  30. How will you declare an array of three function pointers where each function receives two ints and returns a float? Ans : float (*fptr[3])(int,int);
  31. what is a NULL Pointer? Whether it is same as an uninitialized pointer? Ans : pointer with value 0 is called null pointer.It is different from uninitialized pointer
  32. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro? Ans : for questions 31 and 32 refer wikianswers
  33. What does the error ‘Null Pointer Assignment’ mean and what causes this error? Ans : it is run time error when ever our program tries to access 0th location i.e. write or read form that location.
  34. What is near, far and huge pointers? How many bytes are occupied by them? Ans : refer wikianswers
  35. How would you obtain segment and offset addresses from a far address of a memory location? Ans : refer wikianswers
  36. Are the expressions arr and &arr same for an array of integers? Ans : not same wikianswers
  37. Does mentioning the array name gives the base address in all the contexts? Ans : It will always treat.
  38. Explain one method to process an entire string as one unit? Ans : refer wikianswers
  39. What is the similarity between a Structure, Union and enumeration? Ans :  as far as I know they are user defined data types.
  40. Can a Structure contain a Pointer to itself? Ans : yes
  41. How can we check whether the contents of two structure variables are same or not? Ans :
  42. How are Structure passing and returning implemented by the complier? Ans : it is compiler dependent. Some compilers push structures to stack and other will operate with passing address.
  43. How can we read/write Structures from/to data files?
  44. What is the difference between an enumeration and a set of pre-processor # defines? Ans : enumerations will have continuous values if values are not assigned but #defines should have values predefines
  45. what do the ‘c’ and ‘v’ in argc and argv stand for? Ans : “argc” stands for “argument count”. It is the number of elements in “argv”, “argument vector”. (While “vector” and “array” mean different things in java, traditionally they are synonyms.) reference
  46. Are the variables argc and argv are local to main? Ans : yes
  47. What is the maximum combined length of command line arguments including the space between adjacent arguments? Ans : will be compiler dependent.
  48. If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?
  49. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? Ans: refer wikianswers
  50. What are bit fields? What is the use of bit fields in a Structure declaration? Ans : refer wikianswers
  51. To which numbering system can the binary number 1101100100111100 be easily converted to? Ans : Hexa decimal or octal
  52. Which bit wise operator is suitable for checking whether a particular bit is on or off? Ans : bit wise and
  53. Which bit wise operator is suitable for turning off a particular bit in a number? Ans : bit wise and.
  54. Which bit wise operator is suitable for turning on a particular bit in a number? Ans : bit wise or.
  55. which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1? Ans : Left shifting by one for unsigned number for signed left shift by 1 and restore the most significant bit.
  56. Write a program to compare two strings without using the strcmp() function.
    Ans :
    signed int funs32strcmp(const char * ps8string1, const char * ps8string2)
    {
    unsigned int u32stringlen1 = 0,u32stringlen2= 0;
    if((ps8string1 == NULL) ||(ps8string2 = NULL))
    return(-1);
    while(*ps8string1 != ”)
    u32stringlen1 ++;
    u32stringlen1 ++;
    while(*ps8string2 != ”)
    u32stringlen2 ++;
    u32stringlen2 ++;
    if(u32stringlen2 != u32stringlen1)
    return(-1);
    while(u32stringlen1 != 0)
    {
    u32stringlen1–;
    if( (*(ps8string1+u32stringlen1)) !=  (*(ps8string2+u32stringlen2)))
    return(-1);
    }
    return(0);
    }
  57. Write a program to concatenate two strings.
    Ans :
    signed int funs32strcat(char * ps8dststr,const char * ps8secstr)
    {
    char * ps8TmpDstString = ps8dststr;
    const char * ps8TmpSrcString = ps8secstr;
    if( (ps8dststr == NULL) || (ps8secstr == NULL))
    {
    return(-1);
    }
    while((*ps8TmpDstString) != ”)
    ps8TmpDstString ++;do
    {
    *ps8TmpDstString = *ps8TmpSrcString;
    ps8TmpSrcString ++;
    ps8TmpDstString ++;
    }while((*ps8TmpSrcString) != ”);
    }
  58. Write a program to interchange 2 variables without using the third one.
    Ans :
    int main()
    {
    int var1,var2;
    scanf(“%d %d”,&var1,&var2);
    var1^=var2^=var1^=var2;
    return(0);
    }
  59. Write programs for String Reversal & Palindrome check
    Ans : refer wikianswers and wikianswers
  60. Write a program to find the Factorial of a number
    Ans :  refer wikianswers
  61. Write a program to generate the Fibinocci Series
    Ans : refer wikianswers
  62. Write a program which employs Recursion
    Ans :  refer wikianswers
  63. Write a program which uses Command Line Arguments
    Ans : refer wikianswers
  64. Write a program which uses functions like strcmp(), strcpy()? etc
  65. What are the advantages of using typedef in a program?
    Ans : refer wikianswers
  66. How would you dynamically allocate a one-dimensional and two-dimensional array of integers?
    Ans : refer wikianswers
  67. How can you increase the size of a dynamically allocated array?
    Ans : by using realloc;
  68. How can you increase the size of a statically allocated array?
    Ans : refer wikianswers
  69. When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?
  70. Which function should be used to free the memory allocated by calloc()?
    Ans : free(pointer returned by calloc);
  71. How much maximum can you allocate in a single call to malloc()?
    Ans :
  72. Can you dynamically allocate arrays in expanded memory?
    Ans :
  73. What is object file? How can you access object file?
    Ans : object file is a pre-compiled file,but not linked.If you want to access object file you need to access it by using header file to the object file, and at the time of linking you need to provide the object file.
  74. Which header file should you include if you are to develop a function which can accept variable number of arguments?
    Ans : stdarg.h
  75. Can you write a function similar to printf()?
    Ans : refer wikianswers
  76. How can a called function determine the number of arguments that have been passed to it?
    Ans : no such function.
  77. Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
    Ans : no
  78. How do you declare the following:
    • An array of three pointers to chars -> char *(ps8arr[3]);
    • An array of three char pointers  -> char *(ps8arr[3]);
    • A pointer to array of three chars char * p;
    • A pointer to function which receives an int pointer and returns a float pointer -> float *(*function)(int *);
    • A pointer to a function which receives nothing and returns nothing void (*function)();
  79. What do the functions atoi(), itoa() and gcvt() do?
  80. Does there exist any other function which can be used to convert an integer or a float to a string?
  81. How would you use qsort() function to sort an array of structures?
    Ans:
    //write in ascending order
    #include <stdio.h>
    #include <stdlib.h>
    int compare(const void * var1,const void * var2)
    {
    if(*(int *)var1 > *(int *)var2)
    {
    return(1);
    }
    else{
    return(0);
    }
    }
    void main()
    {
    unsigned int au32Nos[10] = {32,44.,55,66,11,8,9,7,9,10};
    qsort(au32Nos,10,4,compare);printf(“%d %d %d %d %d %d %d %d %d %d”,au32Nos[0],au32Nos[1],au32Nos[2],au32Nos[3],au32Nos[4],au32Nos[5],au32Nos[6],au32Nos[7],au32Nos[8],au32Nos[9]);
    }//program to write in descending order
    #include <stdio.h>
    #include <stdlib.h>
    int compare(const void * var1,const void * var2)
    {
    if(*(int *)var1 > *(int *)var2)
    {
    return(0);
    }
    else{
    return(1);
    }
    }
    void main()
    {
    unsigned int au32Nos[10] = {32,44.,55,66,11,8,9,7,9,10};
    qsort(au32Nos,10,4,compare);

    printf(“%d %d %d %d %d %d %d %d %d %d”,au32Nos[0],au32Nos[1],au32Nos[2],au32Nos[3],au32Nos[4],au32Nos[5],au32Nos[6],au32Nos[7],au32Nos[8],au32Nos[9]);
    }

  82. How would you use qsort() function to sort the name stored in an array of pointers to string?
  83. How would you use bsearch() function to search a name stored in array of pointers to string?
  84. How would you use the functions sin(), pow(), sqrt()?
  85. How would you use the functions memcpy(), memset(), memmove()?
  86. How would you use the functions fseek(), freed(), fwrite() and ftell()?
  87. How would you obtain the current time and difference between two times?
  88. How would you use the functions randomize() and random()?
  89. How would you implement a substr() function that extracts a sub string from a given string?
  90. What is the difference between the functions rand(), random(), srand() and randomize()?
  91. What is the difference between the functions memmove() and memcpy()?
    Ans : refer wikianswers
  92. How do you print a string on the printer?
  93. Can you use the function fprintf() to display the output on the screen?

8 thoughts on “C interview questions

  1. Frank Scurley అంటున్నారు:

    I dont know If I said it already but …Great site…keep up the good work.🙂 I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks,🙂

    …..Frank Scurley

  2. Srikanth అంటున్నారు:

    Hi, you have done great work, and I found all questions are quiet useful. But I’m stumbled at using ‘register’ storage class. I know how to use it wisely…:)

    It should also be noted that defining ‘register’ goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register – depending on hardware and implimentation restrictions.

    My question is ‘How do we get to know programmatically if the variable stored in register?’

    if anybody have the code, please let me know (dhondi.srikanth@gmail.com). Thanks in advance.

    1. Hi Srikanth,
      Okay thanks.
      and answer to your question how can I know it is in register or in stack
      #include
      #include

      int main()
      {
      int s32temp1;
      register s32reg1,s32reg2,s32reg3,s32reg4,s32reg5,s32reg6,s32reg7,s32reg8,s32reg9,as32reg[32];
      int s32temp2;
      printf(“%x %x\n”,&s32temp1,&s32temp2);
      printf(“%d \n”,((&s32temp1) – (&s32temp2)));
      if(((&s32temp1) – (&s32temp2)) != 1)
      {
      printf(“all are not register variable\n”);
      printf(“the no of non register variables are %d \n”,((&s32temp1) – (&s32temp2)));
      }
      else
      {
      printf(“all are register variables\n”);
      }
      s32temp1 = 0;
      s32temp2 = 0;
      s32reg1 = 0;
      s32reg2 = 0;
      s32reg3 = 0;
      s32reg4 = 0;
      s32reg5 = 0;
      s32reg6 = 0;
      s32reg7 = 0;
      s32reg8 = 0;
      s32reg9 = 0;
      }

      run this program on the machine and you can find how many are allocated in register and how many are in stack this works fine on GCC,
      and even you can refer the following Wiki page http://en.wikipedia.org/wiki/Processor_register#Some_examples which tells for intel architecture only 8 registers are available but compiler makes a good choice by using code analysis if suppose s32reg1 is used and s32reg9 is not used until you stopped using s32reg1 it may allocate s32reg9 in register memory.when comes to micro controllers compiler tries to create the variable in registers than in stack
      Thanks,
      Prasad

  3. dp అంటున్నారు:

    Hi

    thanks for your effort in keeping this page. please tell me how to read the below statcounter(not only this page but from any html page) into a register or any memory location using a C program.

    dp
    thanks in advance

స్పందించండి

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / మార్చు )

Twitter picture

You are commenting using your Twitter account. Log Out / మార్చు )

Facebook photo

You are commenting using your Facebook account. Log Out / మార్చు )

Google+ photo

You are commenting using your Google+ account. Log Out / మార్చు )

Connecting to %s