Here is another set of challenging questions that could be asked in C language interviews. Interviewers and interviewees can use questions and answers in this blog as a reference guide before conducting/attending an interview. Question.46 is about basics of header files in C. It tests whether the candidate under test is aware of the best practice of declaring rather than defining functions in a header file. Question.47 tests the candidate’s ability to discern between strdup and strcpy functions included in string.h header file. Question.48 is about hte subtle differences between for loop and while loop in C. Question.49 tests an advanced concept of pointer notation of multi-dimensional arrays. Question.50 is a question on bitwise operators in C. It tests the candidate’s knowledge of the operator used for left shift and right shift. See all C interview questions in this blog in one page.
- What are header files? Are functions declared or defined in header files ?
- What is the difference between the functions strdup and strcpy in C?
- What is difference between for loop and while loop in C language?
- Write down the equivalent pointer expression for referring the same element as a[i][j][k][l].
- Which one is equivalent to multiplying an unsigned int by 2, left shifting by 1 or right shifting by 1?
- What are header files? Are functions declared or defined in header files ? Functions and macros are declared in header files. Header files would be included in source files by the compiler at the time of compilation.
- What is the difference between the functions strdup and strcpy in C?
- What is difference between for loop and while loop in C language?
- Write down the equivalent pointer expression for referring the same element as a[i][j][k][l]. Explain.
- Which one is equivalent to multiplying an unsigned int by 2,left shifting by 1 or right shifting by 1?
Header files are included in source code using #include directive.#include<some.h> includes all the declarations present in the header file ‘some.h’.
A header file may contain declarations of sub-routines, functions, macros and also variables which we may want to use in our program. Header files help in reduction of repetitive code.
Syntax of include directive:
#include<stdio.h> //includes the header file stdio.h, standard input output header into the source code
Functions can be declared as well as defined in header files. But it is recommended only to declare functions and not to define in the header files. When we include a header file in our program we actually are including all the functions, macros and variables declared in it.
In case of pre-defined C standard library header files ex(stdio.h), the functions calls are replaced by equivalent binary code present in the pre-compiled libraries. Code for C standard functions are linked and then the program is executed. Header files with custom names can also be created. Read more about header files
Program: Custom header files example
/**************** Index: restaurant.h ****************/ int billAll(int food_cost, int tax, int tip);
/****************
Index: restaurant.c
****************/
#include<stdio.h>
int billAll(int food_cost, int tax, int tip) {
int result;
result = food_cost + tax + tip;
printf("Total bill is %d\n",result);
return result;
}
/****************
Index: main.c
****************/
#include<stdio.h>
#include"restaurant.h"
int main() {
int food_cost, tax, tip;
food_cost = 50;
tax = 10;
tip = 5;
billAll(food_cost,tax,tip);
return 0;
}
strcpy function: copies a source string to a destination defined by user. In strcpy function both source and destination strings are passed as arguments. User should make sure that destination has enough space to accommodate the string to be copied.
‘strcpy’ sounds like short form of “string copy”.
Syntax:
strcpy(char *destination, const char *source);
Source string is the string to be copied and destination string is string into which source string is copied. If successful, strcpy subroutine returns the address of the copied string. Otherwise, a null pointer is returned.
Program: Example program.
#include<stdio.h>
#include<string.h>
int main() {
char myname[10];
//copy contents to myname
strcpy(myname, "nodalo");
//print the string
puts(myname);
return 0;
}
Output:
Explanation:
If the string to be copied has more than 10 letters, strcpy cannot copy this string into the string ‘myname’. This is because string ‘myname’ is declared to be of size 10 characters only.
In the above program, string “nodalo” is copied in myname and is printed on output screen.
strdup function: duplicates a string to a location that will be decided by the function itself. Function will copy the contents of string to certain memory location and returns the address to that location. ‘strdup’ sounds like short form of “string duplicate”
Syntax:
strdup (const char *s);
strdup returns a pointer to a character or base address of an array. Function returns address of the memory location where the string has been copied. In case free space could not be created then it returns a null pointer. Both strcpy and strdup functions are present in header file <string.h>
Program: Program to illustrate strdup().
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char myname[] = "nodalo";
//name is pointer variable which can store the address of memory location of string
char* name;
//contents of myname are copied in a memory address and are assigned to name
name = strdup(myname);
//prints the contents of 'name'
puts(name);
//prints the contents of 'myname'
puts(myname);
//memory allocated to 'name'is now freed
free(name);
return 0;
}
Output:
nodalo
Explanation:
string myname consists of “nodalo” stored in it. Contents of myname are copied in a memory address and memory is assigned to name. At the end of the program, memory can be freed using free(name);
Back to top
for loop: When it is desired to do initialization, condition check and increment/decrement in a single statement of an iterative loop, it is recommended to use ‘for’ loop.
Syntax:
{
//block of statements
increment or decrement
}
Program: Program to illustrate for loop
#include<stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
//print the number
printf("\n %d", i);
}
return 0;
}
Output:
2
3
4
5
Explanation:
The loop repeats for 5 times and prints value of ‘i’ each time. ‘i’ increases by 1 for every cycle of loop.
while loop: When it is not necessary to do initialization, condition check and increment/decrement in a single statement of an iterative loop, while loop could be used. In while loop statement, only condition statement is present.
Syntax:
#include<stdio.h>
int main() {
int i = 0, flag = 0;
int a[10] = { 0, 1, 4, 6, 89, 54, 78, 25, 635, 500 };
//This loop is repeated until the condition is false.
while (flag == 0) {
if (a[i] == 54) {
//as element is found, flag = 1,the loop terminates
flag = 1;
}
else {
i++;
}
}
printf("Element found at %d th location", i);
return 0;
}
Output:
Explanation:
Here flag is initialised to zero. ‘while’ loop repeats until the value of flag is zero, increments i by 1.
‘if’ condition checks whether number 54 is found. If found, value of flag is set to 1 and ‘while’ loop terminates.
Back to top
Consider a multidimensional array a[w][x][y][z].
In this array, a[i] gives address of a[i][0][0][0] and a[i]+j gives the address of a[i][j][0][0]
Similarly, a[i][j] gives address of a[i][j][0][0] and a[i][j]+k gives the address of a[i][j][k][0]
a[i][j][k] gives address of a[i][j][k][0] and a[i][j][k]+l gives address of a[i][j][k][l]
Hence a[i][j][k][l] can be accessed using pointers as *(a[i][j][k]+l)
where * stands for value at address and a[i][j][k]+l gives the address location of a[i][j][k][l].
Program: Example program to illustrate pointer denotation of multi-dimensional arrays.
#include<stdio.h>
#include<string.h>
int main() {
int a[3][3][3][3];
//it gives address of a[0][0][0][0] .
printf(" \n address of array a is %u", a);
printf("\n address of a[2][0][0][0] is %u ,given by a[2], %u given by a+2",
a[2], a + 2);
printf("\n address of a[2][2][0][0] is %u ,given by a[2][2], %u given by a[2]+2",
a[2][2], a[2] + 2);
printf("\n address of a[2][2][1][0] is %u ,given by a[2][2][1] , %u given by a[2][2]+1",
a[2][2][1], a[2][2] + 1);
return 0;
}
Output:
address of a[2][0][0][0] is 65448, given by a[2] , 65448 given by a+2
address of a[2][2][0][0] is 65484, given by a[2][2] ,65484 given by a[2]+2
address of a[2][2][1][0] is 65490, given by a[2][2][1] , 65490 given by a[2][2]+1
Explanation:
This output may differ from computer to computer as the address locations are not same for every computer.
Back to top
Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2.
Eg1: 14<<1;
consider a number 14—–00001110 (8+4+2)is its binary equivalent
left shift it by 1————–00011100(16+8+4) which is 28.
Eg2: 1<<1;
consider the number as 1—00000001(0+0+1).
left shift that by 1————00000010(0+2+0) which is 2.
left shift by 1 bit of a number=2*number
left shift by 1 bit of 2*number=2*2*number
left shift by n bits of number=(2^n)*number
Program:
Program to illustrate left shift and right shift operations.
#include<stdio.h>
int main(void)
{
int x=10,y=10;
printf("left shift of 10 is %d \n",x<<1);
printf("right shift of 10 is %d \n",y>>1);
return 0;
}
Output:
right shift of 10 is 5
Explanation:
Left shift (by 1 position) multiplies a number by two. Right shift divides a number by 2.
More about bitwise operators
Back to top
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |

