C interview questions – recursion, unions vs structures, storage allocation of variables.


c interview questions Below are six of the most frequently asked C language questions in an entry level / experienced interview in software companies. Question.6 (recursion & factorial program) is frequently asked in fresher’s or entry level interview in C language. Recursion and factorial are interviewers’ favourite to start off in an interview. Question.7(binary to hex) is not purely a C language question. It tests whether the candidate is aware of hexa-decimal and binary representation. Binary and Hexa decimal representation is often used in Embedded System programming in C language. Question.8 is about the differences between structures and unions. Many confuse sturctures and unions. It is a good way to test whether the candidate knows the subtle differences between the both. Question.9 is about the memory advantage of Unions over structures. Question.10, 11 are the most essential basics for a C programmer viz. scope and storage allocation of variables. Any C programmer fairly acquainted with the language must be able to answer these two. See all C interview questions in this blog in one page.

  1. What is recursion? Write a program using recursion (factorial)?
  2. Recursion: A function is called ‘recursive’ if a statement within the body of a function calls the same function. It is also called ‘circular definition’. Recursion is thus a process of defining something in terms of itself.

    Program: To calculate the factorial value using recursion.

    #include <stdio.h>
    int fact(int n);
    
    int main() {
     int x, i;
     printf("Enter a value for x: \n");
     scanf("%d", &x);
     i = fact(x);
     printf("\nFactorial of %d is %d", x, i);
     return 0;
    }
    
    int fact(int n) {
     /* n=0 indicates a terminating condition */
     if (n <= 0) {
      return (1);
     } else {
      /* function calling itself */
      return (n * fact(n - 1));
      /*n*fact(n-1) is a recursive expression */
     }
    }

    Download Code

    Output:

    Enter a value for x:

    4

    Factorial of 4 is 24

    Explanation:

    fact(n) = n * fact(n-1)
           If n=4
           fact(4) = 4 * fact(3)  there is a call to fact(3)
           fact(3) = 3 * fact(2)
           fact(2) = 2 * fact(1)
           fact(1) = 1 * fact(0)
           fact(0) = 1
    
           fact(1) = 1 * 1 = 1
           fact(2) = 2 * 1 = 2
           fact(3) = 3 * 2 = 6
           Thus fact(4) = 4 * 6 = 24

    Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.
    Back to top

  3. To which numbering system, can the binary number 1101100100111100 be easily converted to?
  4. 1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ‘A’ to ‘F’. The alphabets represent numbers 10 to 15 respectively. Hexa-decimal numbers are preceeded by ’0x’.

    1101,1001,0011,1100
          1101 = D
          1001 = 9
          0011 = 3
          1100 = C
    1101,1001,0011,1100 = 0xD93C

    Thus the given binary number 1101100100111100 in hexadecimal form is 0xD93C
    Back to top

  5. What are the differences between structures and unions?
  6. Structures and Unions are used to store members of different data types.

    STRUCTURE UNION
    a)Declaration:

    struct
      {
       data type member1;
       data type member2;
      };
    a)Declaration:

    union
      {
       data type member1;
       data type member2;
      };
    b)Every structure member is allocated memory when a structure variable is defined.
    Example:

    struct emp {
     char name[5];
     int age;
     float sal;
    };
    
    struct emp e1;

    Memory allocated for structure is 5+4+4=13 bytes(assuming sizeof int is 4, float is 4, char is 1). 5 byte for name, 4 bytes for age and 4 bytes for sal.

    b)The memory equivalent to the largest item is allocated commonly for all members.
    Example:

    union emp1 {
     char name[5];
     int age;
     float sal;
    };
    
    union emp1 e2;

    Memory allocated to a union is equal to size of the largest member. In this case, char name[5] is the largest-sized member. Hence memory allocated to this union is 5 bytes.

    c)All structure variables can be initialized at a time

    struct st {
     int a;
     float b;
    };
    struct st s = { .a=4, .b=10.5 };

    Structure is used when all members are to be independently used in a program.

    c)Only one union member can be initialized at a time

    union un {
     int a;
     float b;
    };
    
    union un un1 = { .a=10 };

    Union is used when members of it are not required to be accessed at the same time.

    Back to top

  7. What are the advantages of using unions?
  8. Union is a collection of data items of different data types.
    It can hold data of only one member at a time though it has members of different data types.

    If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.

    Example:

    union pen {
     char name;
     float point;
    };

    Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type.
    Union is efficient when members of it are not required to be accessed at the same time.
    Back to top

  9. What is scope & storage allocation of global and extern variables? Explain with an example
  10. Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.

    Example:

    /***************
    Index: f1.c
    ****************/
    #include <stdio.h>
    extern int x;
    
    int main() {
     printf("value of x %d", x);
     return 0;
    }

    Download Code

    /***************
    Index: f2.c
    ****************/
    int x = 3;

    Here, the program written in file f1.c has the main function and reference to variable x. The file f2.c has the declaration of variable x. The compiler should know the datatype of x and this is done by extern definition.

    Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.

    Example:

    #include <stdio.h>
    int x = 0;
    /* Variable x is a global variable.
    It can be accessed throughout the program */
    void increment(void) {
     x = x + 1;
     printf("\n value of x: %d", x);
    }
    
    int main(){
     printf("\n value of x: %d", x);
     increment();
     return 0;
    }

    Download Code
    Back to top

  11. What is scope & storage allocation of static, local and register variables? Explain with an example.
  12. Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access.
    Example: loop counter variables.

    register int y=6;

    Static variables: Memory is allocated at the beginning of the program execution and it is reallocated only after the program terminates. The scope of the static variables is local to the block in which the variables are defined.

    Example:

    #include <stdio.h>
    void decrement(){
      static int a=5;
      a--;
      printf("Value of a:%d\n", a);
    }
    
    int main(){
     decrement();
     return 0;
    }

    Download Code

    Here ‘a’ is initialized only once. Every time this function is called, ‘a’ does not get initialized. so output would be 4 3 2 etc.,

    Local variables: are variables which are declared within any function or a block. They can be accessed only by function or block in which they are declared. Their default value is a garbage value.
    Back to top


About the Author:  This post was written by Madhulika Reddy. You can connect with Madhulika on Orkut.



All comments of post - "C interview questions – recursion, unions vs structures, storage allocation of variables.":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #4  Sridhar

    @Shraddha,
    Will surely take up your request. C++, Java, datawarehousing interview questions are on my todo list.
    Please consider subscribing to this blog http://subscribe.interviewmantra.net
    Thanks,
    The Editor,
    Interview Mantra

    09/09/27 16:04
  2. #3  ~ * k2 * ~

    Thankyu so much for this compilation!
    Is it possible to have similar compilation for C++ basic questions or am i demanding way too much? :-)
    ~Shraddha.

    09/09/25 21:11
  3. #2  Sridhar

    Thanks Venu… Please give us your suggestions also so that we can improve.

    09/04/08 12:56
  4. #1  venu kashyap

    dude nice job man …. i know it would have taken u a lot of time writing all those answers well looking at dem i feel u made the job easier for engineering 1st year students

    09/01/17 01:40

Show all Show 5 so far Close all

Comment begin from here or jump up!