C programming interview questions and answers.
Below are 10 basic C programming interview questions and answers that should be prepared and practiced before an interview. I will post Q & A on c arrays, sorting and searching algorithms, loops, operators, user defined functions and other most useful logic in my next post.

Contents covered
- Pointers in c programming
- Number patterns in c programming
- Searching algorithms in c programming
- Sorting algorithms in C programming
Pointers in c programming
(C programming interview questions and answers)
1. What is the output of this C code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int main() { char *p = NULL; char *q = 0; if (p){ printf(" p "); }else{ printf("nullp"); } if (q){ printf("q\n"); }else{ printf(" nullq\n"); } } |
A. nullp nullq
B. Depends on the compiler
C. x nullq where x can be p or nullp depending on the value of NULL
D. p q
Answer: Option A
2. What is the output of this C code?
1 2 3 4 5 6 7 8 9 |
int main() { int i = 10; void *p = &i; printf("%d\n", (int)*p); return 0; } |
A. Compile time error
B. Segmentation fault/runtime crash
C. 10
D. Undefined behaviour
Answer: Option A
3. What is the output of this C code?
1 2 3 4 5 6 7 8 9 |
int main() { int i = 10; void *p = &i; printf("%f\n", *(float*)p); return 0; } |
A. Compile time error
B. Undefined behaviour
C. 10
D. 0.000000
Answer: Option D
4. What is the output of this C code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int *f(); int main() { int *p = f(); printf("%d\n", *p); } int *f() { int *j = (int*)malloc(sizeof(int)); *j = 10; return j; } |
A. 10
B. Compile time error
C. Segmentation fault/runtime crash since pointer to local variable is returned
D. Undefined behaviour
Answer: Option A
5. What is the output of this C code?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int *f(); int main() { int *p = f(); printf("%d\n", *p); } int *f() { int j = 10; return &j; } |
A. 10
B. Compile time error
C. Segmentation fault/runtime crash
D. Undefined behaviour
Answer: Option A
6. What is the output of this C code?
1 2 3 4 5 6 7 8 9 |
int main() { int *ptr, a = 10; ptr = &a; *ptr += 1; printf("%d,%d/n", *ptr, a); } |
A. 10,10
B. 10,11
C. 11,10
D. 11,11
Answer: Option D
7. Comment on the following?
1 2 3 |
const int *ptr; |
A. You cannot change the value pointed by ptr
B. You cannot change the pointer ptr itself
C. Both (a) and (b)
D. You can change the pointer as well as the value pointed by it
Answer: Option A
8. Which is an indirection operator among the following?
1 2 3 |
A. & B. * C. -> D. . |
A. &
B. *
C. ->
D. .
Answer: Option B
9. Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0)?
A. int *ptr = &a;
B. int *ptr = &a – &a;
C. int *ptr = a – a;
D. All of the mentioned
Answer: Option A
10. What is the output of this C code?
1 2 3 4 5 6 7 8 9 10 |
int x = 0; void main() { int *ptr = &x; printf("%p\n", ptr); x++; printf("%p\n ", ptr); } |
A. Same address
B. Different address
C. Compile time error
D. Varies
Answer: Option A
Number patterns in c programming
(C programming interview questions and answers)
11. Number Pattern 1
1 2 3 4 5 6 7 |
12345 1234 123 12 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i, j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d",j); } printf("\n"); } return 0; } |
12. Number pattern 2
1 2 3 4 5 6 7 |
12345 2345 345 45 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i, j; for(i=1;i<=5;i++) { for(j=i;j<=5;j++) { printf("%d",j); } printf("\n"); } return 0; } |
13. Number pattern 3
1 2 3 4 5 6 7 |
54321 4321 321 21 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i, j; for(i=5;i>=1;i--) { for(j=i;j>=1;j--) { printf("%d",j); } printf("\n"); } return 0; } |
14. Number pattern 4
1 2 3 4 5 6 7 |
54321 5432 543 54 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int i, j; for(i=1;i<=5;i++) { for(j=5;j>=i;j--) { printf("%d",j); } printf("\n"); } return 0; } |
15. Number pattern 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
1234 2341 3421 4321 int main() { int i,j,k; for(i=1;i<=4;i++) { k = i-1; for(j=i;j<i+4;j++) { if(j<=4) { printf("%d",j); } else { printf("%d",k--); } } printf("\n"); } return 0; } |
16. Number pattern 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
11111 0000 111 00 1 #include < stdio.h > int main() { int i, j; for (i = 5; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("%d", i % 2); } printf("\n"); } return 0; } |
17. Number pattern 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 #include < stdio.h > int main() { int i, j; for (i = 1; i <= 5; i++) { for (j = 5; j > i; j--) { printf(" "); } for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } return 0; } |
18. Number pattern 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 int main() { int i, j, n=5; for(i=n; i>1; i--) { for(j=n;j>=1;j--) { if(j>i) printf("%d ", j); else printf("%d ", i); } for(j=2;j<=n;j++) { if(j>i) printf("%d ", j); else printf("%d ", i); } printf("\n"); } for(i=1; i<=n; i++) { for(j=n;j>=1;j--) { if(j>i) printf("%d ", j); else printf("%d ", i); } for(j=2;j<=n;j++) { if(j>i) printf("%d ", j); else printf("%d ", i); } printf("\n"); } return 0; } |
19. Number pattern 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
1 2 2 3 3 4 4 3 3 2 2 1 int main() { int i, j, k; for (i = 1; i <= 4; i++) { for (j = 4; j >= (i - 1) * 2 - 1; j--) printf(" "); printf("%d", i); for (j = 2; j <= (i - 1) * 4; j++) printf(" "); if (i > 1) printf("%d", i); printf("\n"); } for (i = 3; i >= 1; i--) { for (j = 4; j >= (i - 1) * 2 - 1; j--) printf(" "); printf("%d", i); for (j = 2; j <= (i - 1) * 4; j++) printf(" "); if (i > 1) printf("%d", i); printf("\n"); } return 0; } |
20. Number pattern 10
N=39714
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
3 9 1 7 4 9 1 7 4 1 7 4 7 4 4 #include <stdio.h> int main() { long n = 39714, i=1; for(i=10;i<n;i*=10); for (i=i/10; n>0; i/=10) { printf("%d\n", n); n%=i; } return 0; } |
Searching algorithms in c programming
(C programming interview questions and answers)
Linear Search
Linear search is the most simple search algorithm. Here each element is traversed one by one linearly. One of the major advantage of this search is that it takes deliberately less time if the element is present at start. But it takes a lot of time is the element is present in the last.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d is not present in array.\n", search); return 0; } |
Binary Search
This algorithm is much more faster than linear search. Binary search is faster than linear search but list should be sorted, hashing is faster than binary search and perform searches in constant time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.\n", search); return 0; } |
Sorting algorithms in C programming
Bubble Sort
One of the simplest sorting techniques is used here. In bubble sort sorting of elements start from start of the array to end by comparing and switching each pair of array elements. The comparing and switching of pairs occurs n number of times where n is the length of array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> int main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; } |
Insertion Sort
If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/*Sorting Elements of an array in ascending order using insertion sort algorithm*/ #include<stdio.h> int main() { int data[100],n,temp,i,j; printf("Enter number of terms(should be less than 100): "); scanf("%d",&n); printf("Enter elements: "); for(i=0;i<n;i++) { scanf("%d",&data[i]); } for(i=1;i<n;i++) { temp = data[i]; j=i-1; while(temp<data[j] && j>=0) /*To sort elements in descending order, change temp<data[j] to temp>data[j] in above line.*/ { data[j+1] = data[j]; --j; } data[j+1]=temp; } printf("In ascending order: "); for(i=0; i<n; i++) printf("%d\t",data[i]); return 0; } |
Quick Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/*c program for quick sorting*/ #include<stdio.h> #include<conio.h> void qsort(int arr[20], int fst, int last); int main() { int arr[30]; int i,size; printf("Enter total no. of the elements : "); scanf("%d",&size); printf("Enter total %d elements : \n",size); for(i=0; i<size; i++) scanf("%d",&arr[i]); qsort(arr,0,size-1); printf("Quick sorted elements are as : \n"); for(i=0; i<size; i++) printf("%d\t",arr[i]); getch(); return 0; } void qsort(int arr[20], int fst, int last) { int i,j,pivot,tmp; if(fst<last) { pivot=fst; i=fst; j=last; while(i<j) { while(arr[i]<=arr[pivot] && i<last) i++; while(arr[j]>arr[pivot]) j--; if(i<j) { tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp; } } tmp=arr[pivot]; arr[pivot]=arr[j]; arr[j]=tmp; qsort(arr,fst,j-1); qsort(arr,j+1,last); } } |
Merge Sort
Merge Sort is also a divide and algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
/* C program for Merge Sort */ #include<stdlib.h> #include<stdio.h> // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; /* create temp arrays */ int L[n1], R[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge the temp arrays back into arr[l..r]*/ i = 0; // Initial index of first subarray j = 0; // Initial index of second subarray k = l; // Initial index of merged subarray while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy the remaining elements of L[], if there are any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy the remaining elements of R[], if there are any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } /* l is for left index and r is right index of the sub-array of arr to be sorted */ void mergeSort(int arr[], int l, int r) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h int m = l+(r-l)/2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } /* UTILITY FUNCTIONS */ /* Function to print an array */ void printArray(int A[], int size) { int i; for (i=0; i < size; i++) printf("%d ", A[i]); printf("\n"); } /* Driver program to test above functions */ int main() { int arr[] = {12, 11, 13, 5, 6, 7}; int arr_size = sizeof(arr)/sizeof(arr[0]); printf("Given array is \n"); printArray(arr, arr_size); mergeSort(arr, 0, arr_size - 1); printf("\nSorted array is \n"); printArray(arr, arr_size); return 0; } |
Heap Sort
In heap sort we build a heap of elements from the given element then partially sort the heap by removing the largest element from the end of the heap. After removing the largest element, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from the end of the partially sorted array. This is repeated until there are no items left in the heap and the sorted array is full.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/* c program for heap sorting method*/ #include<stdio.h> #include<conio.h> void manage(int *, int); void heapsort(int *, int, int); int main() { int arr[20]; int i,j,size,tmp,k; printf("\n\t------- Heap sorting method -------\n\n"); printf("Enter the number of elements to sort : "); scanf("%d",&size); for(i=1; i<=size; i++) { printf("Enter %d element : ",i); scanf("%d",&arr[i]); manage(arr,i); } j=size; for(i=1; i<=j; i++) { tmp=arr[1]; arr[1]=arr[size]; arr[size]=tmp; size--; heapsort(arr,1,size); } printf("\n\t------- Heap sorted elements -------\n\n"); size=j; for(i=1; i<=size; i++) printf("%d ",arr[i]); getch(); return 0; } void manage(int *arr, int i) { int tmp; tmp=arr[i]; while((i>1)&&(arr[i/2]<tmp)) { arr[i]=arr[i/2]; i=i/2; } arr[i]=tmp; } void heapsort(int *arr, int i, int size) { int tmp,j; tmp=arr[i]; j=i*2; while(j<=size) { if((j<size)&&(arr[j]<arr[j+1])) j++; if(arr[j]<arr[j/2]) break; arr[j/2]=arr[j]; j=j*2; } arr[j/2]=tmp; } |