C Interview Questions
The C programming language is the base for all programming languages. C is one of the oldest and still most used programming languages. It is simple to understand and it supports fast compilation. C was developed to create UNIX operating systems. Today, the UNIX operating system is the most used network operating system all over the world.
In this tutorial, we showed some important and trending interview questions of the C programming language. These questions are based on different types of concepts in C. It will help you in getting placed at big companies such as TCS, Infosys, Wipro, Accenture etc.
C Interview Questions and answers
These interview questions will help you to understand the basic concepts of the C programming language.
Q.1 State the difference between typedef and #define.
Ans. With the help of typedef keyword, you can give alternative names to the existing data type. With this, you cannot create a new data type. It is mainly used in structures, unions etc. to reduce the complexity of the program code.
Syntax of typedef:-
typedef data_type name_of_new_data_type;
Example:-
typedef unsigned int new_int;
#define is a preprocessor directive. The main purpose of this is to replace the value before compilation. It gives an alias name for the defined macro.
Syntax of #define:-
#define NAME value
Example:-
#define SIZE 7
Q.2 Tell the difference between long and double data types.
Ans. These both data types have similar purposes but they are different. For storing large integer values, you can use the long data type. And for storing floating point values which have higher precision than float, you can use a double data type.
long data type
Suppose, a programmer wants to use large numbers on their code then they can use the type specifier named long.
Syntax of long:-
long j; // for storing integer values long long o; // for storing integer values long double p; // for storing floating point values
double data type
The main purpose of this is to store decimal numbers with double precision.
The difference between float and double is that the size of float is 4 bytes and the size of double is 8 bytes.
Syntax of double:-
double a;
Q.3 Why should you use macros over functions?
Ans. One big advantage of using macros over functions is that macros are preprocessed. Unlike functions, Macros are processed before the compilation of a program. Functions are not preprocessed. They are simply compiled.
Macros in C
You can say that macro is a piece of code that is replaced by the value of the macro throughout the program. You can define a macro with #define directive. At the end of defining a macro, you don’t have to put a semicolon(;) to terminate it.
Example:-
#include <stdio.h> #define SIDE 4 int main() { int area; area = SIDE*SIDE; printf("Area is: %d",area); return 0; }
Output:-
Area is: 16
Functions in C
Functions are very useful for all the programming languages. With the help of functions, you can divide a large block of code into small blocks of code and you can also execute those blocks of code again and again. It will save your precious time.
Example:-
int sum(int n1, int n2){ int final; final = n1 + n2; return final; }
Q.4 What is the output of the following program?
#include <stdio.h> int main(void) { int data; int val= 1; int arr[4] = { 2, 3, 1, 4}; data = 4 * 4 + arr[val++] - (9 / val); printf("%d", data); return 0; }
Ans. 15
Q.5 Can you give arguments to the main() function?
Ans. Yes, you can give command line arguments to the main function. You can only pass two arguments to the main function which are argc and argv.
Example:-
#include <stdio.h> int main(int argc, char *argv[]) { for(int i=1; i<argc; i++) printf("%s ", argv[i]); printf("\n"); return 0; }
If you give an argument such as “Have a nice day” from the terminal then it will print you the below output.
Output:-
Have a nice day
Q.6 What is the Difference between local and global variables in C.
Ans.
Local Variable in C | Global Variable in C |
Local variables are declared inside the main function. | Global variables are declared outside the main function. |
Declared only for the main function. | Declared for the entire code. |
These are stored in a stack unless specified. | For global variables, the compiler decides the storage location. |
Only be accessible from inside of the main function. | Only accessible from inside and from outside of the main function. |
Q.7 Why C is known as the base for all programming languages?
Ans. Using the C programming language, several compilers and JVM’s are written. Many programming languages have borrowed the syntax and semantics of C like Python, Java, Rust etc. The C programming language gave birth to new concepts such as array, file handling, pointers, functions etc which are used by these languages also.
Q.8 What is the use of printf() and scanf() functions in C?
Ans. The main purpose of this function is to display the output on the screen.
Below are the format specifiers which you can use in the printf() function.
- %d: Used to print an integer value.
- %s: Used to print a string.
- %c: Used to display a character value.
- %f: Used to display a floating point value.
With the help of the scanf() function, you can take input from the user.
Q.9 Why to use static variables in C?
Ans.
- A variable, defined as static, is known as a static variable. If a variable is set to static then the value of that variable does not change between multiple function calls.
- You can use it as a common value which you can share with all the methods.
- You can initialize the static variable only once.
- Mainly the static variable is initialized to zero. It will change if you update the value of that static variable.
Q.10 Why to use functions in C?
Ans.
- You can make use of functions to execute the same block of code again and again.
- You can call a function several times from anywhere in the program code.
- With the help of functions, you can break a big program into smaller programs. In this way, the program code will be more understandable.
Q.11 What is the output of the following program?
#include <stdio.h> int main() { int x, y = 3; func(&x,&y); printf("%d%d",x,y); return 0; } void func(int *x , int*y) { *x = *x * *y; *y = *y * *y; }
Ans. 09
Q.12 What is dynamic memory allocation?
Ans.
- In dynamic memory allocation, the memory is allocated at runtime and you can increase it during executing the program.
- You can use malloc() and calloc() functions to allocate memory at runtime.
- You don’t have to use dynamic pointers to access the memory.
- It provides less memory space to store the variable.
Example:-
int *point= malloc(sizeof(int)*5);
Q.13 Can you compile a program without the main() function?
Ans. Yes, you can compile a program without the main() function but that program cannot be executed. If you want to run and compile a program without the main() function then you can make use of #define.
Example:-
#include<stdio.h> #define start main void start() { printf("Have a nice day!"); }
Q.14 What is the output of the following program?
#include "stdio.h" int fun(int num) { printf("%d",num); return 0; } int main() { fun; return 0; }
Ans. This program won’t throw you an error. But the fun() function won’t get executed. So, the program will print nothing.
Syntax:-
(type_name) expression;
Q.15 Write a program to check if the number is an Armstrong number or not.
Ans.
#include<stdio.h> #include<conio.h> int main() { int num=155,rev,sum=0,tmp; tmp=num; while(num>0) { rev=num%10; sum=sum+(rev*rev*rev); num=num/10; } if(tmp==sum) printf("It is an armstrong number!"); else printf("It is not an armstrong number!"); getch(); }
Output:-
It is not an armstrong number!
Intermediate C Programming Interview Questions
Q.16 Write a program to reverse a number in C?
Ans.
#include<stdio.h> int main() { int number=265, rev=0, remainer; while(number!=0) { remainer=number%10; rev=rev*10+remainer; number/=10; } printf("%d",rev); return 0; }
Output:-
562
Q.17 What is the output of the following program?
#include<stdio.h> #include<stdlib.h> int main() { int x=25,y=65,res; int i; for(i = 1; i <= x && i <= y; i++) { if((x % i == 0) && (y % i == 0)) { res = i; } } printf("%d",res); return 0; }
Ans. 5
Q.18 Why should you use the sprintf() function in C?
Ans. The sprintf() function stands for string print. You cannot display the output on the screen using this function. It helps you to transfer the data into the buffer.
Syntax:-
int sprintf ( char *string, const char *name, ... );
Example:-
#include<stdio.h> int main() { char string[20]; int s1=sprintf(string,"TechVidvan"); printf("Value of s1: %d",s1); return 0; }
Q.19 Why does n + + execute faster than the n+1?
Ans. n++ needs only one variable. It is a unary operator. n+1 is a binary operation that adds overhead to take more time. In modern compilers, the execution of time of both n++ and n+1 are equivalent.
Example:-
#include<stdio.h> #include<stdlib.h> int main() { int x=2; printf("%d\n", x++); printf("%d",++x); return 0; }
Output:-
2
4
Q.20 Why does the C programming language not support function overloading?
Ans. The C programming language does not support strict typing. In C, many things are implicitly convertible to each other. In C, if you introduce function overloading then you should provide a name mangling technique to prevent the name clashes. When you compile a C program code, the symbol names will remain intact.
Q.21 Can you point out the error in the following code?
#include<stdio.h> int main() { char c; int x; scanf("%c", &c); scanf("%d", &x); printf("%c %d", c, x); return 0; }
Ans. The above program will give you an error because you will not get input for the second scanf() statement.
Q.22 Why to use the new and delete operators in C?
Ans. The new operator is mainly used for allocating memory. With the help of this operator, you can return a pointer to the beginning of the new block of the allocated memory.
Example:-
int *ptr; ptr = new int[20];
The delete operator is mainly used for deallocating memory. With the help of this operator, you can free the memory so that it will be available for other purposes.
Example:-
delete ptr;
Q.23 State the difference between call by value and call by reference.
Ans.
Call by Value | Call by Reference |
In this method, the actual values are passed to the function calls. | In this method, instead of the actual values, the address of the corresponding values are passed to the function calls. |
Actual and Formal parameters are created at different memory locations. | Actual and Formal parameters are created at the same memory locations. |
You cannot change the value of the actual parameter. | You can change the value of the actual parameters by changing the formal parameter. |
Example:- call by value
#include<stdio.h> void func(int n) { n=n+50; printf("%d\n", n); } int main() { int a=10; func(a); return 0; }
Output:-
60
Example:- call by reference
#include<stdio.h> void func(int *n) { (*n) += 10; printf("%d\n", *n); } int main() { int a=10; func(&a); return 0; }
Output:-
20
Q.24 What is the output of the following program code?
#include<stdio.h> int fac(int); int npr(int, int); int main() { int num=5, r=4; int res; res = npr(num, r); printf("%d\n",res); return 0; } int npr(int n1, int n2) { return (fac(n1)/fac(n1-n2)); } int fac(int number) { if(number == 1 || number == 0) return 1; else return number*fac(number-1); }
Ans. 120
Q.25 Suppose, you created a file named Tech.txt. Then what will be the name of that file after executing the below program?
#include <stdio.h> int main() { int res; char o[] = "Tech.txt"; char n[] = "Advance.txt"; res = rename(o, n); if (res == 0) puts("success"); else perror("Error"); return 0; }
Ans. Advance
Advanced C Interview Questions and Answers
Q.26 What is the difference between getch() and getche()?
Ans. The getch() function is used to read a single character from the keyboard. The entered data will not display on the screen.
Example:- getch()
#include <stdio.h> #include <conio.h> int main() { printf("Have a nice day!"); getch(); return 0; }
The above program will terminate immediately.
The getche() function is used to read a single character from the keyboard. But the data will be displayed on the screen.
Example:- getche()
#include <stdio.h> #include <conio.h> int main() { printf("Have a nice day!"); getche(); return 0; }
The above program will terminate immediately too.
Q.27 What is the output of the following program?
#include<stdio.h> #include<conio.h> void main() { int i,j,k; for(i=1;i<=4;i++) { for(j=4;j>=i;j--) printf(" "); for(k=1;k<=i;k++) printf("#"); printf("\n"); } getch(); }
Ans.
#
##
###
####
Q.28 Can you tell me the errors occurred on the following program?
#include <stdio.h> int main() { int b=20,a=20; int res = add(b,a); printf("%d",res) return 0; }
Ans. The function add() is not defined and there is no semicolon at the end of the printf() function.
Q.29 Is it possible to override a defined macro? If yes, then how?
Ans. Yes, you can. For this, you have to use #ifdef and #undef preprocessors. Follow the code below:-
#ifdef A #undef A #endif #define SIZE 5
In the above, we defined a macro named SIZE. It can be undefined using the #undef and you can define it again using the #define.
Q.30 Why to use the ‘\0’ character in a string?
Ans. The main purpose of the ‘\0’ character is to terminate a string. This is known as a null character. It automatically gets placed at the end of a string. It determines the end of the string.
Example:-
char *s ="Hi"; // it is actually Hi\0
Q.31 Can you convert a queue to a stack? If “Yes” then how?
Ans. Yes, you can convert a queue into a stack. You will need 2 stacks to create a queue from it. In C, you can do it in 2 ways:-
- Make the enqueue operation costly.
- Make the dequeue operation costly.
Implementing a stack using two queues
If you want to implement a stack using two queues then you need to make use of the stack operations with the help of queue operations:
- push(element A):-
- If queue1 is empty, enqueue A to queue1
- If queue1 is not empty then enqueue all the elements from queue1 to queue2.
- pop:-
- Dequeue an element from queue1
Queue1 is the main source for the stack. And Queue2 acts as a helper queue.
Q.32 Without using the semicolon to terminate the printf() function, how can you print the “TechVidvan” string?
Ans. You can use the if statement to print a string without using the semicolon at the end of the printf() function.
Example:- printf() function
#include<stdio.h> int main() { if(printf("TechVidvan")) {} return 0; }
Output:-
TechVidvan
Q.33 Can you guess the output of the following program?
#include<stdio.h> #include<conio.h> void main() { int a=10,b=4; a>>=2; b=a; printf("%d ",b); getch(); }
Ans. 2
Q.34 What is the output of the following program code?
#include<stdio.h> #include<conio.h> void main() { int x=-4,y=-20,z=-10; if((x>y)&&(x>z)) printf("x is bigger!"); if((y>z)&&(y>x)) printf("y is bigger"); if((z>x)&&(z>y)) printf("z is bigger"); getch(); }
Ans. x is bigger.
Q.35 What is the output of the following program?
#include <stdio.h> int main() { void *ptr; int i=2; int *p=&i; ptr=p; printf("%d",(int*)*ptr); return 0; }
Ans. It will give you a compile error. Because you cannot apply indirection on type void*.
Summary
You have to practice more to improve your coding skills in C. You should master the basic concepts of the C programming language. In this tutorial, we discussed some interview or job level questions of the C programming language and we also gave their answers. These questions will help you to prepare for the interview. And you will get better knowledge of the C programming language.