In this articles you are going to learn C interview questions which are divided into basic, experienced and advanced.
Table of Contents
Basic C interview questions
The basic C interview questions that interviewers ask frequently to the freshers.
1. What is a C language?
Ans: C is a middle-level programming language developed and designed by Dennis Ritchie in 1972. Initially, C was developed and implemented on the UNIX operating system. But now C runs on many operating systems like Windows, Mac OS, etc. C is a procedural language commonly known as a structured programming language.
2. Who developed the C language? and when? (Frequently asked C interview question)
Ans: In 1972, at bell laboratories, An American computer scientist Dennis Ritchie developed c language by taking B and BCPL languages as a base.
3. Why c is a middle-level language?
Ans: C provides facilities of both high-level language and low-level language. C is a high-level language because the syntax of the high-level language is generally designed in the way by which the programmer easily writes the code. C also supports low-level language such as bitwise operators.
4. Tell me 5 features of the C programming language
Ans:
C is a compatible language – It is simple to implement and highly effective.
C consists of a rich set of built-in functions.
It provides dynamic allocation of memory – this allows to allocate memory at runtime.
C language provides modular programming – It is a logical collection of one or more functions or modules.
C supports pointers that can directly access the address of a variable in the computer’s memory.
5. What are the limitations of c programming language?
Ans:
C does not support inheritance makes it more complex to use because everything has to be written from scratch.
The switch statement does not allow float data type
C provides only one major memory allocation function: malloc
The switch statement does not allow logical operators
Pointers in c are difficult to handle. It may happen by mistake that you may access and overwrite some critical memory.
6. What is the structure of c programming language?
Ans:
Declaration section
Link section
Definition section
Global declaration section
main() function section
{
Variable declaration / initialisation;
Program statements;
}
Subprogram section
Function1()
.
.
Functionn()
7. How many keywords are reserved in the c language?
Ans: There are 32 keywords reserved in the c language.
8. What is an escape sequence?
Ans: escape sequences are nothing but the backslash character which is used in output functions. Eg. \t (horizontal tab), \n (newline), etc.
9. What is an Ascii number?
Ans: ASCII is the abbreviation for American Standard Code for Information Interchange. It is used to represent the numbers, alphabets, and special symbols
10. What is the ASCII number of character A and a?
Ans: ASCII number of A is 65 and ASCII number of a is 97.
11. What are the different types of operators?
Ans: C provides arithmetic operators, logical operators, bitwise operators, relational operators, unary operators, assignment operators, and special operators.
12. Explain the difference between logical AND/OR bitwise AND/OR and
Ans:
Logical AND / OR | Bitwise AND / OR |
Logical AND is used as ‘&&’ and logical OR is used as ‘||’ | Bitwise AND is used as ‘&’ and bitwise OR is used as ‘|’ |
Logical AND / OR is used when we consider two conditions | Bitwise AND / OR is used when we consider two bits |
Logical AND / OR return output in form of true or false | Bitwise AND / OR return output in form of bits i.e 0 or 1 |
13. What is the use of functions in the C language?
Ans: To break complex code in blocks we use functions. Functions help to find errors and debug code easily. Also, to avoid writing the same code again and again in main() we can use functions. We can call it the number of times in our code.
14. What is a recursion in the C language?
Ans: The function that calls itself repeatedly until some specific condition satisfies is termed as a recursive function.
15. What is an array in the c language?
Ans: Array is a set of data items of the same data type which share the same name. It is a linear data structure that means the data is stored in sequential order in the memory.
16. Explain pointers
Ans: Pointers are the variables that contain the address which is the location of another variable in the memory. Pointers increase the execution time and reduce the length and complexity of the program. C provides many types of pointers like a NULL pointer, Void pointer, etc.
17. What is dynamic memory allocation? and how to allocate dynamic memory?
Ans: The process of allocation memory at run time is known as dynamic memory allocation. Dynamic memory allocation avoids memory wastage. We can allocate memory dynamically using built-in malloc and calloc functions.
18. What is a structure in c language?
Ans: Structure is a collection of logically related data items of different data types grouped under a single name. It is analogous to records. As the records contain different fields, the data items that make up a data structure are called members or fields.
19. How to travel loops infinite times?
Ans: the syntax of for loop to travel infinite times
for(;;)
{
//statements
}
syntax of while loop to travel infinite times
while(1)
{
//statements
}
syntax of the do-while loop to travel infinite times
do
{
//statements
}while(1);
20. What is type casting in c language?
Ans: Conversion of one type of data into another type of data is known as type casting
21. Which function is used for file opening in the C language?
Ans: fopen() is used to open a file. It returns a file pointer. It takes 2 parameters, the first is the file name and the second is the mode of a file.
22. What is the use of auto keyword in the C language?
Ans: Auto is a default storage class for local variables. The auto variables are created when the function is called and destroyed automatically when the function is exited, hence the name automatic.
23. What is a header file in c language?
Ans: Headers files are the files that contain function declaration and macro definitions. It has a .h extension. There are two types of header files, first, that provides a C compiler like stdio.h, conio.h, etc. The second is the user-defined header file.
24. What is the difference between java programming and c programming?
Ans: The major difference between java and c language is java is an object-oriented language and c is a procedure-oriented language. Moreover, Java is a high-level programming language and c is a middle-level programming language. Java follows bottom to top approach whereas, c follows top to bottom approach.
25. What are pre-increment and post-increment?
Ans: The pre-increment operator adds 1 into the operand and then the value assigns to the variable. Whereas, post-increment operator first assigns the value to the variable and then adds 1 in the operand.
26. Is the c language case sensitive?
Ans: Yes, C language is case sensitive.
27. What is the sizeof() in C language?
Ans: it is a built-in function and returns the number of bites of the parameter passed to it.
28. What is the use of exit() in the C language?
Ans: To terminate the program immediately and return to the operating system exit() is used.
29. State the dissimilarities between getchar() and getch() in c language
Ans: getchar() is a standard function on the stdio library that reads a character from the keyboard and prints it. Whereas, The getch() is not a standard function from the conio header file which reads a character from the keyboard and does not print it.
30. By which operator we can access the members of the structure?
Ans: In C language, Structure members are accessed with the (.) dot operator.
C Programming Interview Questions for Experienced
31. What is a static keyword in the C language?
Ans: If a variable is declared non-static then its scope is limited if you want to use a variable in the whole program to declare a static variable. We can declare both functions and variables as static in c language.
32. What is the difference between call by reference and call by value?
Ans: In call by reference, the content of the arguments in the calling function get changed i.e. the original values are changed. Instead of passing the value of the variable, we can pass the memory address of the variable to the function. Whereas, in the call, by value, the content of the arguments in the calling functions are not changed, even if they are changed in the function. The content of the actual parameters gets copied into the corresponding formal parameters.
33. What is pointer to pointer in c language?
Ans: A pointer to pointer is a variable that stores the memory location of another variable in it.
34. State the difference between malloc() and calloc()
Ans: malloc() allocates the requested size of bytes and returns the pointer to the first byte. On the other hand, calloc() allocates memory to the array elements and initializes them to zero, and then returns the pointer to the memory.
35. What is the difference between auto and static storage classes?
Ans:
auto | static |
Auto variables persist till the control remains within the block where it is defined. | Static variables persist between different function calls. |
The auto variables are created when the function is called and destroyed automatically when the function is exited. | The variables persist in the main memory. |
The by default storage class for local variables is considered to be auto | For global variables static is the default storage class |
Default initial value: garbage value | Default initial value: zero |
The auto declaration is valid only for variables | The static declaration is valid for variables and functions |
Eg. auto int x; | Eg. static int x; |
36. How to swap numbers stored in two variables without using a third variable in C programming?
Ans:
#include<stdio.h>
main()
{
int x=1;
int y=8;
printf("Before Swapping a=%d b=%d",x,y);
x=x+y;//x=30
y=x-y;//y=10
x=x-y;//x=20
printf("\nAfter swapping a=%d b=%d",x,y);
}
Output:
Before swapping a=1 b=8
After swapping a=8 b=1
37. The dissimilarity between Dynamic and Static memory allocation
Ans:
Static memory allocation | Dynamic memory allocation |
Memory allocation for the variables done at compile time is called static memory allocation. | Memory allocation for the variables done at run time (execution time) is called dynamic memory allocation |
The Memory wastage if allocated memory is not occupied. | No memory wastage since fixed memory which is required is allocated at the run time only |
Problems arise when more than defined memory is required. | Whatever memory is required is defined when the execution of the program. |
The data structure is a fixed kind. | Data structure grows as well as shrink as per requirement. |
At the time of execution, no extra memory is allocated or unallocated. | At the time of execution memory binding or destruction is also possible. |
Once the memory is allocated for the variables they remain permanently allocated. | The allocation of memory in a program is done at run-time |
Slower execution | Faster execution |
More memory space is required. | Less memory space is required. |
38. What are the C preprocessors?
Ans: Preprocessor is a program that processes our source program before it is passed to the compiler.
39. How to call a function before main() in C language
Ans: To call the function before main() pragma directive is used.
#include<stdio.h>
#pragma startup fun
void fun()
{
printf("In fun\n");
}
main()
{
printf("In main\n");
}
Output
In fun
In main
NOTE: The GCC compiler does not support the pragma directive. So the output will be.
In main
40. Write a code to find the factorial on a given number. Take input from the user.
Ans:
#include <stdio.h>
main()
{
int n,i;
long int fact = 0;
printf("Enter an integer number: ");
scanf("%d",&n);
fact=1;
for(i=n; i>=1; i--)
fact=fact*i; //factorial logic
printf("\nFactorial of %d is = %ld",n,fact);
}
Output
Enter an integer number: 5
Factorial of 5 is = 120
41. Write a program to copy a string without using built-in function strcpy()
Ans:
#include <stdio.h>
void stringcpy(char* s1,char* s2)
{
int i=0;
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
}
main()
{
char str1[100],str2[100];
printf("Enter string 1: ");
scanf("%[^\n]s",str1);
stringcpy(str2,str1); //calling to the function
printf("String 1: %s \nString 2: %s\n",str1,str2);
}
Output
Enter string 1: Pawan
String 1: Pawan
String 2: Pawan
42. Write a hollow square pattern in c language
Ans:
#include <stdio.h>
main()
{
int i,j;
int n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1 ||i==n||j==1||j==n)
{
printf("* ");
}
else
printf(" ");
}
printf("\n");
}
}
Output
Enter the number of rows:10
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
43. What is the difference between structure and union in the C language?
Ans :
Structure | Union |
Each member in the Structure is assigned its unique storage area. | All members of the Union share the same storage area in the computer’s memory. |
Allocates the memory equal to total memory (sum of the memory)required by all the members. | Allocates the memory equal to the maximum memory required by the member. |
All members can be active at a time. | Only one member can be active at a time. |
All members of a structure variable can be initialized. | Only the first member of a union variable can be initialized. |
Occupies a lot of memory space. | Conservation of memory. |
44. What are the command line arguments?
Ans: Arguments that are passed to the program when the program is invoked are called command-line arguments. That is, these arguments are passed from the command line during run time.
When there is a need to pass information into a program while you are running it, then the information can be passed into the main() function through the two built-in arguments :
int argc :
Argument count which contains the number of arguments in the command line(number of arguments passed to the program), including the command name.
Char *argv[] :
An argument vector is an array that contains the address of each argument. It is an array of character pointers that point to the command line arguments. The size of this array is equal to the value of argc.
45. Write a program to find the minimum and maximum of two numbers without using any loop or condition
Ans:
#include <stdio.h>
main()
{
int a = 8;
int b = 18;
printf("max = %d\n", ((a+b)+abs(a-b))/2);
printf("min = %d\n", ((a+b)-abs(a-b))/2);
}
Output
max = 18
min = 8
46. Write program for pascal triangle in C language
Ans:
#include <stdio.h>
main() {
int r, c = 1, s, i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
for (i = 0; i < r; i++) {
for (s = 1; s <= r - i; s++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
c = 1;
else
c = c * (i - j + 1) / j;
printf("%4d", c);
}
printf("\n");
}
}
Output
Enter the number of rows: 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
47. Write a program to change the lower case characters to upper case character without using toupper()
Ans :
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String(Lower case):");
scanf("%s",str);
int i=0;
while(str[i]!='\0')
{
if(str[i]>96 && str[i]<123 )
str[i]-=32;
i++;
}
printf("Upper case String is :%s",str);
}
Output
Enter your String(Lower case):pawan misal
Upper case String is :PAWAN MISAL
48. Write a program to change the upper case characters to lower case character without using tolower()
Ans:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String(Upper case):");
scanf("%s",str);
int i=0;
while(str[i]!='\0')
{
if(str[i]>64 && str[i]<90 )
str[i]+=32;
i++;
}
printf("Lower case String is :%s",str);
}
Output
Enter your String(Upper case):PAWAN MISAL
Lower case String is :pawan misal
Advanced C interview questions for the interview
The advanced C interview questions that interviewers ask frequently are listed below.
49. Which data type can we use to store 32768?
Ans: The range of the int data type is -32767 to 32767. To store 32768 we need a long data type.
50. Does the C compiler show an error if we add one header file twice in the same program?
Ans: If the syntax of adding a header file is correct then the C compiler compiles the first line of the header file and ignores the second.
51. What is the difference between while(0) and while(1) in c language?
Ans: While(1) is an infinite loop that means the statements inside the loop execute infinite times whereas, while(0) is the exact opposite of while(1), it means that the condition will always be false and the statements inside it will not execute once. The program will not get executed.
52. Can we write more than one main() in the C program?
Ans: No, we can not add to main() in a single program. Because the main() c compiler starts its execution and if you add two main() then the compiler will get confused.
53. What happens when you compile a C program on your system?
Ans: Compiler takes the source file and generates object file and then the object file is passed to the linker. The linker links the library files with the object files and generates a single executable file.
54. Write a program to check if two numbers are equal or not without using comparison operators.
Ans: To check two numbers are equal use the XOR operator if they are equal XOR will return zero
#include <stdio.h>
main()
{
int a = 8;
int b = 8;
if(!a ^ b)
{
printf("Numbers are equal");
}
else
{
printf("Numbers are not equal");
}
}
Output
Numbers are equal
55. At the time of accepting strings, why don’t we write & sign in C language?
Ans: Because the address of the string variable is already stored in the string that’s why we do not need (&) ampersand sign in c language.