C Interview Questions
C Interview Questions
Ans: The Datatypes in C Language are broadly classified into 4 categories. They are
as follows:
Basic Datatypes
Derived Datatypes
Enumerated Datatypes
Void Datatypes
The Basic Datatypes supported in C Language are as follows:
Datatype Name Datatype Size Datatype Range
Q15. What is the purpose of printf() and scanf() in C Program?
Ans: printf() is used to print the values on the screen. To print certain values, and
on the other hand, scanf() is used to scan the values. We need an appropriate
datatype format specifier for both printing and scanning purposes. For example,
%d: It is a datatype format specifier used to print and scan an integer value.
%s: It is a datatype format specifier used to print and scan a string.
%c: It is a datatype format specifier used to display and scan
a character value.
%f: It is a datatype format specifier used to display and scan a float value.
Q16. What is an array?
Ans. The array is a simple data structure that stores multiple elements of the same
datatype in a reserved and sequential manner. There are three types of arrays,
namely,
One Dimensional Array
Two Dimensional Array
Multi-Dimensional Array
Int Array = [1,2,3,4,5,6,65]; //integer
Q17. What is /0 character?
Ans: The Symbol mentioned is called a Null Character. It is considered as the
terminating character used in strings to notify the end of the string to the
compiler.
String --> array ["a","b","\0"]
Q18. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the
Machine Code in one shot. On the other hand, Interpreter is used in Java
Programming Langauge and other high-end programming languages. It is
designed to compile code in line by line fashion.
Q19. Can I use int datatype to store 32768 value?
Ans: No, Integer datatype will support the range between -32768 and 32767. Any
value exceeding that will not be stored. We can either use float or long int.
Intermediate C Programming Interview Questions
Q20. How is a Function declared in C Language?
Ans: A function in C language is declared as follows,
1 return_type function_name(formal parameter list)
2 {
3 Function_Body;
4 }
Q21. What is Dynamic Memory allocation? Mention the syntax.
Ans: Dynamic Memory Allocation is the process of allocating memory to the
program and its variables in runtime. Dynamic Memory Allocation process involves
three functions for allocating memory and one function to free the used memory.
10;
malloc() – Allocates memory
Syntax:
1 ptr = (cast-type*) malloc(byte-size);
calloc() – Allocates memory
Syntax:
1 ptr = (cast-type*)calloc(n, element-size);
realloc() – Allocates memory
Syntax:
1 ptr = realloc(ptr,
newsize);
free() – Deallocates the used memory
Syntax:
1 free(ptr);
Q22. What do you mean by Dangling Pointer Variable in C Programming?
Ans: A Pointer in C Programming is used to point the
memory location of an existing variable. In case if
that particular variable is deleted and the Pointer is
still pointing to the same memory location, then that
particular pointer variable is called as a Dangling
Pointer Variable.
Q23. Where can we not use &(address operator in C)?
Ans: We cannot use & on constants and on a variable which is declared using
the register storage class.
Q24. Write a simple example of a structure in C Language
Ans: Structure is defined as a user-defined data type that is designed to store
multiple data members of the different data types as a single unit. A structure will
consume the memory equal to the summation of all the data members.
1 struct employee
2 {
3 char name[10];
4 int age;
5 }e1;
6 int main()
7 {
8 printf("Enter the name");
9 scanf("%s",e1.name);
10 printf("n");
11 printf("Enter the age");
12 scanf("%d",&e1.age);
13 printf("n");
14 printf("Name and age of the employee: %s,%d",e1.name,e1.age);
15 return 0;
16 }
Q25. Differentiate between call by value and call by reference
Ans:
Factor Call by Value Call by Reference
Q42. How can you print a string with the symbol % in it?
Ans: There is no escape sequence provided for the symbol % in C. So, to print %
we should use ‘%%’ as shown below.
1 printf(“there are 90%% chances of rain tonight”);
Q43. Write a code to print the following pattern.
1
12
123
1234
12345
Ans: To print the above pattern, the following code can be used.
1 #include<stdio.h>
2 int main()
3 {
4 for(i=1;i<=5;1++)
5 {
6 for(j=1;j<=5;j++)
7 {
8 print("%d",j);
9 }
10 printf("n");
11 }
12 return 0;
13 }
Q44. Explain the # pragma directive.
Ans: The following points explain the Pragma Directive.
This is a preprocessor directive that can be used to turn on or off certain
features.
It is of two types #pragma startup, #pragma exit and pragma warn.
#pragma startup allows us to specify functions called upon program startup.
#pragma exit allows us to specify functions called upon program exit.
#pragma warn tells the computer to suppress any warning or not.
Q45. How can you remove duplicates in an array?
Ans: The following program will help you to remove duplicates from an array.
1 #include <stdio.h>
2 int main()
3 {
4 int n, a[100], b[100], calc = 0, i, j,count;
5 printf("Enter no. of elements in array.n");
6 scanf("%d", &n);
7 printf("Enter %d integersn", n);
8 for (i = 0; i < n; i++)
9 scanf("%d", &a[i]);
10 for (i = 0; i<n; i++)
11 {
12 for (j = 0; j<calc; j++)
13 {
14 if(a[i] == b[j])
15 break;
16 }
17 if (j== calc)
18 {
19 b[count] = a[i];
20 calc++;
21 }
22 }
23 printf("Array obtained after removing duplicate elementsn");
24 for (i = 0; i<calc; i++)
25 {
26 printf("%dn", b[i]);
27 }
28 return 0;
29 }
//Output
Enter no. of elements in array. 5
Enter 5 integers
12
11
11
10
4
Array obtained after removing duplicate elements
12
11
10
4
Q46. What is Bubble Sort Algorithm? Explain with a program.
Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the
list, compares adjacent elements and swaps them if they are in the wrong order.
The pass through the list is repeated until the list is sorted.
The following code executes Bubble Sort.
1 int main()
2 {
3 int array[100], n, i, j, swap;
4 printf("Enter number of elementsn");
5 scanf("%d", &n);
6 printf("Enter %d Numbers:n", n);
7 for(i = 0; i<n; i++)
8 scanf("%d", &array[i]);
9 for(i = 0 ; i<n - 1; i++)
10 {
11 for(j = 0 ; j < n-i-1; j++) { if(array[j]>array[j+1])
12 {
13 swap=array[j];
14 array[j]=array[j+1];
15 array[j+1]=swap;
16 }
17 }
18 }
19 printf("Sorted Array:n");
20 for(i = 0; i < n; i++)
21 printf("%dn", array[i]);
22 return 0;
23 }
Q47. What is Round-robin algorithm? Write a code for Round Robin
Scheduling
Ans: Round-robin Algorithm is one of the algorithms employed by process and
network schedulers in computing in order to evenly distribute resources in the
system.
The following code will execute Round Robin Scheduling
1 #include<stdio.h>
2
3 int main()
4 {
5 int i, limit, total = 0, x, counter = 0, time_quantum;
6 int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
7 float average_wait_time, average_turnaround_time;
8 printf("nEnter Total Number of Processes:t");
9 scanf("%d", &limit);
10 x = limit;
11 for(i = 0; i<limit; i++)
12 {
13 printf("nEnter Details of Process[%d]n", i + 1);
14 printf("Arrival Time:t");
15 scanf("%d", &arrival_time[i]);
16 printf("Burst Time:t");
17 scanf("%d", &burst_time[i]);
18 temp[i] = burst_time[i];
19 }
20
21 printf("nEnter Time Quantum:t");
22 scanf("%d", &time_quantum);
23 printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen");
24 for(total = 0, i = 0; x != 0;)
25 {
26 if(temp[i] <= time_quantum && temp[i] > 0)
27 {
28 total = total + temp[i];
29 temp[i] = 0;
30 counter = 1;
31 }
32 else if(temp[i]>0)
33 {
34 temp[i] = temp[i] - time_quantum;
35 total = total + time_quantum;
36 }
37 if(temp[i] == 0 && counter == 1)
38 {
39 x--;
40 printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total -
41 arrival_time[i] - burst_time[i]);
42 wait_time = wait_time + total - arrival_time[i] - burst_time[i];
43 turnaround_time = turnaround_time + total - arrival_time[i];
44 counter = 0;
45 }
46 if(i == limit - 1)
47 {
48 i = 0;
49 }
50 else if(arrival_time[i + 1] <= total)
51 {
52 i++;
53 }
54 else
55 {
56 i = 0;
57 }
58 }
59
60 average_wait_time = wait_time * 1.0 / limit;
61 average_turnaround_time = turnaround_time * 1.0 / limit;
62 printf("nnAverage Waiting Time:t%f", average_wait_time);
63 printf("nAvg Turnaround Time:t%fn", average_turnaround_time);
64 return 0;
}
//Output
Q48. Which structure is used to link the program and the operating system?
Ans: The answer can be explained through the following points,
The structure used to link the operating system to a program is file.
The file is defined in the header
file “stdio.h”(standard input/output header file).
It contains the information about the file being used, its current size and
its location in memory.
It contains a character pointer that points to the character that is being
opened.
Opening a file establishes a link between the program and the operating
system about which file is to be accessed.
Q49. What are the limitations of scanf() and how can it be avoided?
Ans: The Limitations of scanf() are as follows:
scanf() cannot work with the string of characters.
It is not possible to enter a multiword string into a single variable using
scanf().
To avoid this the gets( ) function is used.
It gets a string from the keyboard and is terminated when enter key is
pressed.
Here the spaces and tabs are acceptable as part of the input string.
Q50. Differentiate between the macros and the functions.
Ans: The differences between macros and functions can be explained as follows:
Macro call replaces the templates with the expansion in a literal way.
The Macro call makes the program run faster but also increases the program
size.
Macro is simple and avoids errors related to the function calls.
In a function, call control is transferred to the function along with arguments.
It makes the functions small and compact.
Passing arguments and getting back the returned value takes time and
makes the program run at a slower rate.