C interview questions – pass by value, pass by reference, enumeration

by Madhulika Reddy on October 2, 2008


c interview questions This post is a set of five of frequently asked interview questions at entry level. These questions have been attempted below. Care has been taken to make answer simple and clear. Question.12, 13 test whether the candidate appearing for interview in C Language is aware of pass by value and pass by reference concepts. The solution for these questions also carries an example for each of the concept. Question.14 is about Enumerations. Enumerations are quite useful and can make the code clear. Question.15 asks the use of typedef in C Language. This question can give a fair idea to the interviewer about the interview candidate. Freshers may not even have heard about typedef. Question.16 is about register variables. Usage of register variables is limited and they may be pretty useful in Embedded System or Micro-controller programming in C language. See all C interview questions in this blog in one page.

  1. What is Pass by Value? Write a C program showing this concept.
  2. Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.

    Example:

    #include <stdio.h>
    
    void swap(int x, int y) {
     int t;
     t = x;
     x = y;
     y = t;
    }
    
    int main() {
     int m = 10, n = 20;
     printf("Before executing swap m=%d n=%d\n", m, n);
     swap(m, n);
     printf("After executing swap m=%d n=%d\n", m, n);
     return 0;
    }

    Download Code

    Output:

    Before executing swap m=10 n=20

    After executing swap m=10 n=20

    Explanation:
    In the main function, value of variables m, n are not changed though they are passed to function ‘swap’. Swap function has a copy of m, n and hence it can not manipulate the actual value of arguments passed to it.
    Back to top

  3. What is Pass by Reference? Write a C program showing this concept.
  4. Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. C does not support Call by reference. But it can be simulated using pointers.

    Example:

    #include <stdio.h>
    /* function definition */
    void swap(int *x, int *y) {
     int t;
     t = *x; /* assign the value at address x to t */
     *x = *y; /* put the value at y into x */
     *y = t; /* put the value at to y */
    }
    
    int main() {
     int m = 10, n = 20;
     printf("Before executing swap m=%d n=%d\n", m, n);
     swap(&m, &n);
     printf("After executing swap m=%d n=%d\n", m, n);
     return 0;
    }

    Download Code

    Output:

    Before executing swap m=10 n=20

    After executing swap m=20 n=10

    Explanation:
    In the main function, address of variables m, n are sent as arguments to the function ‘swap’. As swap function has the access to address of the arguments, manipulation of passed arguments inside swap function would be directly reflected in the values of m, n.
    Back to top

  5. What is an Enumeration?
  6. Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure.

    Example: consider light_status as a data type. It can have two possible values – on or off.

    enum light_status
    {
      on, off
    };
    
    enum light_status bulb1, bulb2;
    /* bulb1, bulb2 are the variables */

    Declaration of enum has two parts:
    a) First part declares the data type and specifies the possible values, called ‘enumerators’.
    b) Second part declares the variables of this data type.
    We can give values to these variables:
    bulb1 = on;
    bulb2 = off;
    Back to top

  7. What is the use of typedef?
  8. typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.
    Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.

    Example:

    struct employee {
     char name[20];
     int age;
    };
    
    struct employee e;

    The above declaration of the structure would be easy to use when renamed using typedef as:

    struct employee {
     char name[20];
     int age;
    };
    
    typedef struct employee EMP;
    EMP e1, e2;

    Back to top

  9. What are register variables? What are advantages of using register variables?
  10. Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register variable is defined. Variable stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register

    Example:
    register int x=5;
    Variables for loop counters can be declared as register. Note that register keyword may be ignored by some compilers.
    Back to top


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


2,274 readers have already subscribed to email updates!

Enter your email address:

  • http://www.cse.buffalo.edu/~mjp44/courses/cse250_sp09/overview.html Mike Prentice

    Thank you for this article, it was very helpful in putting together my own page for my students! (Returning large objects in C++) http://www.cse.buffalo.edu/~mjp44/courses/cse250_sp09/overview.html

  • http://www.interviewmantra.net Sridhar

    We are very glad that our article was of some help to you.

    -Sridhar,
    Interview Mantra

  • Anonymous

    Thanks for the well compiled interview questions and Answers on C.
    Please also visit my blog for Interview Questions and Answers
    http://www.aired.in/search/label/Interview-Questions

    Thanks
    Joya

  • karan

    plz tell me how 2 program the foll pattern:
    1
    4 6
    16 25 36……

  • amit

    *
    ***
    *****
    ***
    *
    plz tell me the coding

  • http://gmail sharief

    thanks a lot

  • http://mycollect.in/wordpress/?p=761 Interview Questions | MyCollect

    [...] [...]

  • Moin 5

    can we write code in main function?
     

  • ishwar

    i need program to print the following pattern ::

    1
    2 3
    4 5 6
    7 8 9 10

Previous post:

Next post: