C Interview Questions
Several jobs require candidates to have a profound knowledge of C. These C Interview Questions have been designed specially to get you acquainted with the nature of questions that you may encounter during your interview for the subject of C.
1. What is C language?
C is a procedural and general-purpose programming language created by Dennis Ritchie in 1972 at the Bell Telephone Laboratories. It was invented to write UNIX operating system. C is the most widely used computer language. The main features of C language include low-level access to memory, easy to learn and modular structure which makes code debugging, maintenance, and testing easier. Due to these features make C language suitable for system programmings like an operating system or compiler development.
2. What are the features of C Programming Language?
Some important features of C programming language are given below:
- Simple Language - C is a simple language. It is very easy to understand and learn.
- Mid-Level Language – C is a mid-level language and enables low-level language featuress like development of system applications such as kernel and drivers. It also supports the feature of high-level language. Hence C is a mid-level language.
- Machine Independent Language – C can be interpreted on various operating systems including UNIX-based systems, Linux, Mac OS and various versions of Windows.
- Case-Sensitive Language - C is a case sensitive language and treats the uppercase and lowercase characters in a different manner.
- Structured Programming Language – C is a structured programming language that means any C program can be achieved in parts using functions. This makes any C program easy to understand and modify.
- Rich Library Support – C provides lots of inbuilt functions which makes programming faster and easier. These functions can be accessed by including appropriate header file in the C program.
- Powerful & Fast Language - C is a fast language as it takes very less time in compilation and execution.
- Dynamic Memory Allocation - C supports the use of pointers which means a user can directly interact with memory and allocate memory dynamically.
3. What is a pointer in C?
A pointer is a variable which stores address of another variable.The address of a variable can be obtained by using ampersand sign (&), known as address-of operator followed by the name of a variable.
The value of a variable can also be accessed by using pointer. The * operator, known as dereference operator followed by the name of the pointer gives the value stored in the address pointed by the pointer.
//p1 is a pointer which stores address of variable p1 = &Var; //*p1 gives value of variable x = *p1;
In the example below, an integer variable called MyVar and a pointer called p1 are created. pointer p1 is assigned the address of MyVar. Then, *p1 is used to get the value of MyVar as shown in the output.
#include <stdio.h> int main (){ int MyVar = 10; int *p1; p1 = &MyVar; printf("%p\n",p1); printf("%i",*p1); return 0; }
The output of the above code will be:
0x7ffed13c2db4 10
4. Write a program to swap two numbers without using the third variable.
This are many ways to swap two numbers. The example below uses + operator to swap two numbers.
#include <stdio.h> static void swap(int, int); static void swap(int x, int y) { printf("Before Swap.\n"); printf("x = %i\n", x); printf("y = %i\n", y); //Swap technique x = x + y; y = x - y; x = x - y; printf("After Swap.\n"); printf("x = %i\n", x); printf("y = %i\n", y); } int main() { swap(10, 25); }
The output of the above code will be:
Before Swap. x = 10 y = 25 After Swap. x = 25 y = 10
5. Write a program to print Fibonacci series.
This are many ways to print the Fibonacci series in C. The example below uses dynamic programming to print the given term of Fibonacci series.
#include <stdio.h> static int fib(int); static int fib(int n) { //creating array which contains Fibonacci terms int f[n+1]; f[0] = 0; f[1] = 1; for(int i = 2; i <= n ; i++) { f[i] = f[i-1] + f[i-2]; } return f[n]; } int main() { printf("Fibonacci 6th term: %i\n",fib(6)); printf("Fibonacci 7th term: %i\n",fib(7)); printf("Fibonacci 8th term: %i\n",fib(8)); }
The output of the above code will be:
Fibonacci 6th term: 8 Fibonacci 7th term: 13 Fibonacci 8th term: 21