C interview questions – static variables, pointers structures


c interview questions Question.1 tests whether the interviewee is aware of significance of a static variable and whether is able to differentiate between a static variable and an automatic variable. Question.2 is about pointers in C language. Question.3(structure) is also a good starter, may be used to test whether the interviewee is aware of structure variables. Question.4(program to print a custom pattern) is a little tough question for an entry level candidate. But if interviewee can answer this question, one can assume he has enough capability to cope up with computer programming. Swapping of two numbers is a very common question, so chances are that many of the interviewees may have known the conventional swapping program by heart. Question.5 is a good variant of popular swapping questions. It tests knowledge of bit-wise operator in C language. Use the links below to directly skip to that question in this page. 40 C interview questions have been posted in this site. See all the C interview questions in this blog in a single page.

Showing 5 out of 65 C Interview Questions.

  1. What does a static variable mean?

  2. A static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialised by using keyword static before variable name.

    For Example:

    static int a = 5;

    A static variable behaves in a different manner depending upon whether it is a global variable or a local variable. A static global variable is same as an ordinary global variable except that it cannot be accessed by other files in the same program / project even with the use of keyword extern. A static local variable is different from local variable. It is initialised only once no matter how many times that function in which it resides is called. It may be used as a count variable.

    Example:

    #include <stdio.h>
    //program in file f1.c
    void count(void) {
     static int count1 = 0;
     int count2 = 0;
     count1++;
     count2++;
     printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2);
    }
    
    /*Main function*/
    int main(){
     count();
     count();
     count();
     return 0;
    }

    Download Code

    Output:

    Value of count1 is 1, Value of count2 is 1
    Value of count1 is 2, Value of count2 is 1
    Value of count1 is 3, Value of count2 is 1

    Back to top

  3. What is a pointer?
    A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators.
    It follows a special arithmetic called as pointer arithmetic.

    A pointer is declared as:

    int *ap;
    int a = 5;
    

    In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.

    Next before ap is used

    ap=&a;

    This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

    Operations on a pointer:

    • Dereferencing operator ‘ * ‘:
    • This operator gives the value at the address pointed by the pointer . For example after the above C statements if we give

      printf("%d",*ap);

      Actual value of a that is 5 would be printed. That is because ap points to a.

    • Addition operator ‘ + ‘:
    • Pointer arithmetic is different from ordinary arithmetic.

      ap=ap+1;

      Above expression would not increment the value of ap by one, but would increment it by the number of bytes of the data type it is pointing to. Here ap is pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending upon the compiler.

  4. A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators. It follows a special arithmetic called as pointer arithmetic.

    A pointer is declared as:

    int *ap;
    int a = 5;

    In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.

    Next before ap is used

    ap=&a;

    This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

    Back to top

  5. What is a structure?
  6. A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:

    int roll_number;
    char name[30];
    int total_marks;

    This concept would be particularly useful in grouping data types. You could declare a structure student as:

    struct student {
     int roll_number;
     char name[30];
     int total_marks;
    } student1, student2;

    The above snippet of code would declare a structure by name student and it initializes two objects student1, student2. Now these objects and their fields could be accessed by saying student1. style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>roll_number for accesing roll number field of student1 object, similarly student2. style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>name for accesing name field of student2 object.

    Back to top

  7. How to print below pattern?
  8.  1
     2 3
     4 5 6
     7 8 9 10

    Program:

    #include <stdio.h>
    
    int main() {
     int i, j, ctr = 1;
     for (i = 1; i < 5; i++) {
      for (j = 1; j <= i; j++){
      printf("%2d ", ctr++);
      }
     printf("\n");
     }
     return 0;
    }

    Download Code

    Explanation:
    There are two loops, a loop inside another one. Outer loop iterates 5 times. Inner loop iterates as many times as current value of i. So for first time outer loop is executed, inner loop is executed once. Second time the outer loop is entered, inner loop is executed twice and so on. And every time the program enters inner loop, value of variable ctr is printed and is incremented by 1. %2d ensures that the number is printed in two spaces for proper alignment.

    Back to top

  9. How to swap two numbers using bitwise operators?
  10. Program:

    #include <stdio.h>
    int main() {
     int i = 65;
     int k = 120;
     printf("\n value of i=%d k=%d before swapping", i, k);
     i = i ^ k;
     k = i ^ k;
     i = i ^ k;
     printf("\n value of i=%d k=%d after swapping", i, k);
     return 0;
    }

    Download Code

    Explanation:

    i = 65; binary equivalent of 65 is 0100 0001
    k = 120; binary equivalent of 120 is 0111 1000
    
    i = i^k;
    i...0100 0001
    k...0111 1000
    ---------
    val of i = 0011 1001
    ---------
    
    k = i^k
    i...0011 1001
    k...0111 1000
    ---------
    val of k = 0100 0001 binary equivalent of this is 65
    ---------(that is the initial value of i)
    
    i = i^k
    i...0011 1001
    k...0100 0001
    ---------
    val of i = 0111 1000 binary equivalent of this is 120
    ---------(that is the initial value of k)

    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 – static variables, pointers structures":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #11  anonymousNo Gravatar

    answer for 4 with just a loop:
    int i,j,k;
    for(i=0, j=1, k=0; i<10; i++)
    {
    printf("%d ", i+1);
    k++;
    if (k == j)
    {
    k = 0;
    j++;
    printf("\n");
    }
    }

    10/04/10 17:39
  2. #10  shubham aggarwalNo Gravatar

    hi friends,
    the questions are really nice.if u found any more such questions then send me on shubham409@gmail.com

    10/02/13 14:26
  3. #9  jack

    hi i am Arunprakash.T really nice this question and this helpful for me if you have more question then send my email address arun.cse4@gmail.com

    09/07/06 04:05
  4. #8  Sridhar

    @Vivek,
    All the questions that you have posted as comment have been answered. Check out the posts.

    09/04/08 12:58
  5. #7  Amol

    nice

    09/03/14 01:31
  6. #6  Vivek

    what is the difference between a for loop and a while loop?

    what is recursion?

    write a program for factorial?

    Thease are very frequently asked in Clanguage in interviews

    08/06/21 09:55
  7. #5  Purnesh

    Hi Sridhar!!

    That’s a very good blog!!
    I wish had I born a few years late, so that I could take advantage of your blog.

    Keep going!

    Purnesh

    08/05/26 08:26
  8. #4  Gopi

    its cool really very usefull fr beginners n others also…. u r doing a great job man…. u jus add hw 2 select da course n projects also..

    07/12/17 09:09
  9. #3  Bhargava

    Your comprehensive passage on learning JAVA has given me a good insight into it as a beginner. The way you have summarized the course and the steps to take an initiative facilitates the readers to easily grasp the matter.Your collection of information as a regular JAVA practitioner (though probably with some cpy/pste) helped me to evict most of the fallacies out of my skull. Your definitely deserve a warm accolade for this compendium. Hope you would put in here the answers for the most reqrd queries of people . Thanks 4 ur patience in maintaining this blog

    07/12/02 08:21
  10. #2  Sridhar

    thanks for criticism. I will give solutions and new things from now on.

    07/08/11 02:13
  11. #1  KranthiNo Gravatar

    is it anything great in that if u
    copy from most common documents any pasting in net

    06/09/03 13:16

Show all Show 5 so far Close all

Comment begin from here or jump up!