C Programming Interview Questions and Answers
Here are some of the most frequently asked C Programming questions in the interview time. Refer this blog further for head "Your Hired!" at the interview time.☺so, let's get star.
1) What is C language?
C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.
_________________________________________________________________________________
2) Why C is known as a mother language?
C language is considered as the mother language of all the modern languages because most of the compilers, JVMs, Kernals etc. are written in C language and most of languages follows c syntax e.g. C++, Java etc.
It provides the core concepts like array, functions, file handling etc. that is being used in many languages like C++, java, C# etc.
_________________________________________________________________________________
3) Why C is called a mid level programming language?
C is called middle-level language because it is actually bind the gap between a machine level language and high-level languages. User can use c language to do System Programming (for writing operating system) as well as Application Programming (for generate menu driven customer billing system ).
_________________________________________________________________________________
4) What are the features of C language?
C is the widely used language. It provides a lot of features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. Structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10.Extensible
_________________________________________________________________________________
5) What is the use of printf() and scanf() functions?
printf scanf in C
The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
_________________________________________________________________________________
6) What is the difference between call by value and call by reference in C?
We can pass value to function by one of the two ways: call by value or call by reference. In case of call by value, a copy of value is passed to the function, so original value is not modified. But in case of call by reference, an address of value of passed to the function, so original value is modified.
_________________________________________________________________________________
7) What is recursion in C?
When function is called within the same function, it is known as recursion in C. The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is know as tail recursion. In tail recursion, we generally call the same function with return statement. An example of tail recursion is given below.
Let's see a simple example of recursion.
recursionfunction()
{
recursionfunction();//calling self function
}
Example of tail recursion in C
Let's see an example to print factorial number using tail recursion in C language.
#include<stdio.h>
#include<conio.h>
int factorial (int n)
{
if ( n < 0)
return -1; /*Wrong value*/
if (n == 0)
return 1; /*Terminating condition*/
return (n * factorial (n -1));
}
void main(){
int fact=0;
clrscr();
fact=factorial(5);
printf("\n factorial of 5 is %d",fact);
getch();
}
Output
factorial of 5 is 120
We can understand the above program of recursive method call by the figure given below:
_________________________________________________________________________________
8) What is pointer in C?
The pointer in C language is a variable, it is also known as locator or indicator that points to an address of a value.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions.
2) We can return multiple values from function using pointer.
3) It makes you able to access any memory location in the computer's memory.
_________________________________________________________________________________
9) What are the usage of pointer in C?
1. Accessing array elements
2. Dynamic memory allocation
3. Call by Reference
4. Data Structures like tree, graph, linked list etc.
_________________________________________________________________________________
10) What is NULL pointer in C?
A pointer that doesn't refer to any address of a value but NULL, is known as NULL pointer. For example:
int *p=null;
_______________________________________________________________________________
11) What is far pointer in C?
A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer.
_______________________________________________________________________________
12) What is dangling pointer in C?
If a pointer is pointing any memory location but meanwhile another pointer deletes the memory occupied by first pointer while first pointer still points to that memory location, first pointer will be known as dangling pointer. This problem is known as dangling pointer problem.
_______________________________________________________________________________
13) What is static and dynamic memory allocation?
static memory allocation | dynamic memory allocation |
---|---|
memory is allocated at compile time. | memory is allocated at run time. |
memory can't be increased while executing program. | memory can be increased while executing program. |
used in array. | used in linked list. |
Static memory allocation
In case of static memory allocation, memory is allocated at compile time and memory can't be increased while executing the program. It is used in array.
Dynamic memory allocation
In case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in linked list.
_________________________________________________________________________________
14) What is the difference between malloc() and calloc()?
malloc(): The malloc() function allocates single block of requested memory. It has garbage value initially.
calloc(): The calloc() function allocates multiple block of requested memory. It initially initializes all bytes to zero.
_______________________________________________________________________________
15) What is the purpose of sprintf() function?
It is used to print the formatted output into char array.
_______________________________________________________________________________
16) Can we compile a program without main() function?
Yes, we can compile but it can't be executed.
But, if we use #define, we can compile and run C program without using main() function. For example:
#include<stdio.h>
#define start main
void start()
{
printf("Hello");
}
_______________________________________________________________________________
17) What is the difference between getch() and getche()?
The getch() function reads a single character from keyboard. It doesn't uses any buffer, so entered data is not displayed on the output screen.
The getche() function reads a single character from keyword but data is displayed on the output screen. Press Alt+f5 to see the entered character.
_______________________________________________________________________________
18) What is the difference between near, far and huge pointers?
A virtual address is composed of selector and offset.
A near pointer doesn't have explicit selector whereas far and huge pointers have explicit selector. When you perform pointer arithmetic on far pointer, selector is not modified but in case of huge pointer it can be modified.
These are the non-standard keywords and implementation specific. These are irrelevant in modern platform.
_______________________________________________________________________________
19) Write a program to print "hello world" without using semicolon?
There are various ways to do so. Let's see a program to print "hello world" using if.
- #include<stdio.h>
- void main(){
- if(printf("hello world")){}
- }
_______________________________________________________________________________
So,these are some important interview question that might be helpful you in your interview.
Here are some C Programs which also ask you in exam. Refer that at least one time.
Comments