C Interview Questions - Set 5

C Programming Interview Questions

This page consists of C Programming interview questions and answers.

Q1: Briefly explain call by value and call by reference in C programming language

We can pass value to a function in two different ways.

  • Call by Value
  • Call by Reference

In call by value a copy of the original value is passed to the function. Any change made to the copy is not reflected back to the original value.

In call by reference a reference of the original value is passed to the function. And any change made to the reference inside the function is reflected back to the original value.

Q2: Implement Call by Value in C programming language

We can write the following C code to showcase Call by Value.

#include <stdio.h>

void foo(int);

int main() {

  int x = 10;
  printf("Inside main function.\n");
  printf("Value of x %d\n", x);

  // calling foo
  foo(x);

  printf("Value of x after function call %d\n", x);

  return 0;

}

void foo(int x) {
  printf("Inside foo function.\n");
  x = x + 10;
  printf("Value of x updated to %d\n", x);
}

The above code will give us the following output.

Inside main function.
Value of x 10
Inside foo function.
Value of x updated to 20
Value of x after function call 10

Click here to learn more about Call by Value.

Q3: Implement Call by Reference in C programming language

We can write the following code to implement call by reference in C programming language.

#include <stdio.h>

void foo(int *);

int main(void) {

  int x = 10;
  printf("Value of x before function call: %d\n", x);

  foo(&x);
  
  printf("Value of x after function call: %d\n", x);
  
  return 0;
}

void foo(int *n) {
  *n = *n + 10;
  printf("Inside foo function: Value updated to %d\n", *n);
}

The output of the code is given below.

Value of x before function call: 10
Inside foo function: Value updated to 20
Value of x after function call: 20

Click here to learn more about Call by Reference.

Q4: What is the output of the following C program?

C program:

#include <stdio.h>

int main(void) {

  int arr[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  int r = 3, c = 3;
  int i, j;
  int sum = 0;
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      if (i == j && i % 2) {
        sum += arr[i][j];
      }
    }
  }
  printf("Sum: %d\n", sum);

  return 0;
}

Answer:

The answer of the above C program is 5.

The code is only adding odd row-column value to variable sum.

Q5: Explain infinite loop

A loop without any exit condition runs indefinitely and is called an infinite loop.

Following are some of the examples of infinite loop.

Infinite for loop.

for(i = 1; i > 0; i++) {
  // some code goes here...
}

Infinite while loop.

i = 1;
while (i > 0) {
  // some code goes here...
  i++;
}

Q6: Find the infinite loop in the following C program snippet

Code #1:

for (;;) {}

Code #2:

while (1) {}

Code #3:

do {} while (1);

Answer:

All of them are infinite loop and there is no exit condition to jump out of the loop body.

Q7: What will be the output of the following C code snippet?

C code snippet:

int x = 5;
int y = 2;
float z = x/y;
printf("z = %f", z);

Answer:

The above C code snippet will print 2.000000.

Even though we know that 5/2 equals 2.5 but since they are stored in integer variable so the final return is also in integer without the decimal part.

Q8: How will you modify the given C code snippet to get the correct result?

C code snippet:

int x = 5;
int y = 2;
float z = x/y;
printf("z = %f", z);

Answer:

We have to use type casting in order to get the correct result. We can type cast x to float data type and then it will work.

Modified C code snippet:

int x = 5;
int y = 2;
float z = (float) x/y;
printf("z = %f", z);

Q9: How is getch and getche functions different for each other?

Both the functions are used to read a single character input from the keyboard.

But getch() function does not displays the input character whereas, getche() function displays the data on the output screen.

Q10: Write C code snippet to allocate 10 blocks of memory for integer type data using malloc function

We can write the following C code snippet to allocate 10 blocks of memory of type int to store integer values.

// int pointer
int *iptr;

// allocate memory
iptr = (int *) malloc (10 * sizeof(int));

Click here to read more about malloc function.