Most Trending C Interview Questions and Answers in 2023
Hard work can be measured by success, and success can’t be achieved without preparation. So we have brought these C interview questions for you which contains different types of questions like coding, tricky and fundamentals. We assure you this tutorial on C Interview Questions and Answers will help you to achieve success as these questions are specially designed by C professionals from various companies who have taken thousands of C interviews in IT industry. These C programming interview questions will help you for getting placed in top companies like TCS, Infosys, Intel, Accenture, etc
If four things are followed – having a great aim, acquiring knowledge, hard work, and perseverance – then anything can be achieved. by A.P.J Abdul Kalam
So, hit the scroll button and come into the race!
C Interview Questions and Answers
If you are a fresher, these C interview questions with answers will help you to build the basic concepts.
Q.1 What is the difference between typedef and #define?
Ans. typedef helps you assign an alternative name for an existing data type. It does not introduce a new data type. We use it mainly while dealing with structures, unions, and enumerations to increase the readability and appeal of the code by reducing its complexity.
The syntax of typedef is:
typedef data_type new_data_type_name;
For instance,
typedef unsigned long int Big_Positive_Integer;
Learn more about typedef in C/C++ with real-time example
#define, on the other hand, is a preprocessor directive and it works by replacing the value before compiling the code. Since it simply gives an alias name for the macro, we do not have to specify the data type of variable we are creating.
The syntax of #define is:
#define VALUE_NAME value;
For instance,
#define MAXIMUM 10
Q.2 How would you find the ASCII value of the uppercase character ‘A’ using the concept of implicit type conversion?
Ans.
#include<stdio.h> int main() { char character = 'A'; int number = 0, value; value = character + number; printf("The ASCII value of A is: %d\n",value); return 0; }
Output-
Now, let us understand how it works:
The ASCII value of ‘A’ is found out to be 65. Using typecasting in C, the compiler automatically converts the character of char data type into the integer data type and the expression value = character + number becomes equal to 65 + 0 = 65
Therefore, the output would be 65.
Q.3 What is the difference between long and double data types?
Ans. Although, both are data types that have a seemingly similar purpose, but are different.
Double is used for storing floating-point values up to a higher precision than float, whereas long is used only for storing large integer value up to a higher precision than int.
Enhance your fundamental skills with Data Types in C with examples
Q.4 What is the difference in the way data is stored in an array and a pointer?
Ans. In C array, data is stored in sequential order. The difference between the memory addresses of the array elements is constant. This constant value depends on its data type.
For instance, this is how the memory address of the array elements are related in the int data type according to a 64-bit compiler:
Clearly, the constant difference between adjacent array elements is 4 bytes, that is, the size of the int data type.
In a pointer, the collection of data is not in sequential order. The pointer stores the memory address of the value to which it points. A relationship between the memory address of pointers cannot be established in terms of a constant difference between its an element and next element.
Here is a diagrammatic representation of how the memory address is stored in pointers in C/C++:
Q.5 What is the advantage of using macros over functions?
Ans. We generally prefer macros before functions as macros are pre-processed. In simple words, macros in C are processed before the compilation of the program.
On the other hand, functions are not preprocessed but simply compiled.
The preprocessing of macros gives it an upper hand over functions.
Q.6 How would you access the address of a pointer to a variable?
Ans. We know that a pointer points to the memory address of a given variable. In order to find out the memory address of the pointer, a pointer to a pointer would be needed to serve the purpose.
Here is a program that will help you access the address of a pointer to a variable:
#include<stdio.h> int main() { int value; int *value_pointer; int **pointer_to_pointer; value_pointer = &value; pointer_to_pointer = &value_pointer; printf("The value of the variable is: %d\n", value); printf("The memory address of the variable is: %p\n", value_pointer); printf("The memory address of the pointer to the variable is: %p\n", pointer_to_pointer); return 0; }
Output-
Q.7 Is it possible to give arguments in the main() function?
Ans. Yes, it is possible to give arguments to the main function. This concept is called command line arguments. Command line arguments in C are nothing but simply arguments that are specified after the name of the program in the system’s command line, and these argument values are passed on to your program during program execution. Only two arguments can be passed to the main function called argc and argv.
Q.8 What is the difference between a local and a global variable?
Ans. The differences between local and global variables are as follows:
Local Variable | Global Variable |
They are declared inside the main function. | They are outside the main function, usually before the main function. |
It is not possible to access these functions outside the main function | They can be accessed anywhere inside the program. |
Local variables are declared only for the main function. | They are declared for the entire program. |
Get a complete guide to learn Variables in C/C++
Q.9 How would you find the length of a string without using the inbuilt function strlen()?
Ans. Here is a program in C that helps you find the length of a string:
#include <stdio.h> int main() { printf("Welcome to DataFlair tutorials!\n\n"); char String[50]; int count; printf("Enter a string: "); scanf("%s", String); for(count = 0; String[count] != '\0'; count++); printf("The length of string: %d\n", count); return 0; }
Output-
C Interview Questions by Industry Experts
Q.10 What is the use of a NULL pointer?
Ans. NULL pointers are used for the initialization of pointers, or to represent the end of a linked list of unknown length. It can also be used to indicate an error while returning a pointer from a function.
It’s the right time to uncover the concept of Linked List in C/C++
Q.11 What is the difference between lvalue and rvalue?
Ans. We use ‘lvalue’ to refer to the objects that appear at the left-hand side or right-hand side of the identifier. In contrast to this, we use ‘rvalue’ to refer to the data value that is stored at an address in memory. It is important to note that rvalue is an expression that cannot have a value assigned to it which means the rvalue appears on right but not on the left-hand side of an assignment operator “=”.
Q.12 What is the use of new and delete operators in C?
Ans. The new operator in C is used for memory allocation. It indicates to return a pointer to the beginning of the new block of memory allocated.
For instance,
int *pointer;
pointer = new int[10];
Here, 10 new blocks of memory are allocated with the help of the new keyword.
The delete operator is used for memory deallocation. We use it when we no longer require the variable to be stored in computer memory. It is responsible to free the memory so that it becomes available for other purposes.
For instance,
delete pointer;
Q.13 Out of the following data types, which are considered as primitive?
float
long
arrays
double
enumeration
Ans. The primitive data types are the fundamental data types available in the C programming language. Basically, they are data types that are not derived from any other data type.
The primitive data types from the above list are a float, long and double.
Q.14 What is the difference between call by value and call by reference?
Ans. The differences between call by value and call by reference are as follows:
Call by Value | Call by Reference |
In this method, a copy of the values is passed to the function calls. | The address of the corresponding values is passed to the function calls instead of the actual values. |
If we make any changes inside the function, these changes would not be reflected in other functions. | If we make any changes inside the function. These changes would be reflected in other functions. |
The actual parameters and formal parameters are created in different memory locations. | The actual parameters and formal parameters are created in the same memory locations. |
Q.15 What are the conditions of binary search?
Ans. There are basically 2 conditions that need to be satisfied before we perform a binary search:
The array must be sorted either in ascending or descending order.
The lower and upper bound and the sort order of the array should be known.
Q.16 What could be wrong with your code is the function malloc() is declared as undefined by the C compiler?
Ans. You might have probably not included the header file <stdlib.h> while using malloc().
Q.17 What would be the output of the following code according to a 16-bit compiler?
#include <stdio.h> int main() { int number = -10; printf(“%d\n”, sizeof(number)); }
Ans. The output would be 2.
According to a 16-bit compiler, the memory occupied by the int data type is 2 bytes whereas the memory occupied by a 32-bit and 64-bit compiler is 4 bytes for the int data type.
Learn the Basic Structure of C Program in 7 Mins
Q.18 What are the different ways to sort numbers in C?
Ans. There are 2 ways to sort numbers in C:
- By implementing user-defined sorting algorithms like the bubble sort, selection sort or insertion sort.
- An inbuilt function in C is available to sort numbers called the qsort() function
Q.19 What is the difference between #define and const?
Ans. #define is a preprocessor directive that we use in order to define a macro containing some value or expression. This value or expression is substituted during program compilation. Evidently, #define does not occupy computer memory, unlike variables.
For instance,
#include<stdio.h> #define LIMIT 5; int main() { int array[LIMIT]; }
We use the const keyword in the declaration of a variable. Unlike #define, it occupies computer memory to store a particular constant value. Once the value has been initialized, it cannot be further modified.
For instance,
const float pi = 3.14;
Q.20 Which header file would you require to access the following inbuilt functions:
- isupper()
- fgets()
- strrev()
- fabs()
Ans.
<ctype.h>
<stdio.h>
<string.h>
<math.h>
Create Header Files in C Within Seconds
Tricky C Programming Interview Questions
Q.21 Predict the output of the following code
#include<stdio.h> int main() { int const value = 10; printf("%d", ++value); return 0; }
Ans. The program would give a compilation error as the value of a constant cannot be changed at any cost.
Output-
Q.22 What is the difference between an actual parameter and a formal parameter?
Ans. The differences between actual and formal parameters are as follows:
Actual Parameters | Formal Parameters |
We use it in function calls. | We use it in the function header. |
The values which we pass through the function definition through the function calls in actual parameters are actual values being passed. | They simply receive the values that are passed to the function through the function calls. |
The values passed to the function may be local or global. | The values passed to the function are necessarily local. |
Q.23 Where is the deletion of elements done in a queue?
Ans. The deletion (dequeue) operation is performed from the front of the queue as it follows the FIFO (First In First Out) rule.
Stacks and Queues in C – Master the Concepts of LIFO & FIFO
Q.24 What is the difference between islower() and tolower() function in C?
Ans. The islower() functions checks if the character or string has all its characters in lower case or not and returns an integral value depending upon the validation of the condition. The tolower() function is used to convert a character or a string to lowercase form.
Q.25 What does the ‘\0’ character indicate in a string?
Ans. The ‘\0’ is a NULL character that indicates the termination of a string. It is automatically appended at the end of the string.
Q.26 What would be the output of the following code:
#include<stdio.h> int main() { int number = 100; printf("%d\n%d\n%d\n",number++,number,++number); }
Ans. Output-
Q. 27 What is the maximum number of characters you can use to name a variable?
Ans. You cannot use more than 32 characters while naming a variable in C provided it follows the rules of naming an identifier.
Q.28 What is the use of comments in C?
Ans. Comments in C are a great way to express the logic behind certain statements useful for the programmer to bookmark the logic used by him.
Tokens in C – An Awesome Concept you can’t Afford to Miss Out
Q.29 What is the difference between ‘g’ and “g” in C?
Ans. In C, ‘g’ is treated as a char data type as it is enclosed within single quotes. “g” is treated as a string data type (array of characters) which is terminated by a ‘\0’ character as it is enclosed within double-quotes.
Q. 30 Is it possible to convert a queue into a stack? If yes, then how?
Ans. It is possible to convert a queue into a stack. 2 stacks would be required for the creation of a queue. It can be done in 2 ways:
- By making the enqueue operation costly.
- By making the dequeue operation costly.
31. Describe function pointers and how callbacks in C may be implemented using them.
Ans. Function pointers are pointers that direct users to a function’s address. They may be used to construct callbacks, in which one function is called inside of another and received as an argument. This enables more extension and flexibility in the programming. Asynchronous programming and event handling both frequently employ callbacks. You may dynamically alter a function’s behaviour to meet various needs by giving function pointers as arguments.
32. What distinguishes the C “malloc” and “calloc” functions, and when would one be preferable to the other?
Ans. In C, memory is dynamically allocated using the “malloc” and “calloc” functions. The primary distinction between the two is that “malloc” allocates memory that has not yet been initialised whereas “calloc” does the opposite. When you need to quickly allocate memory but don’t require it to be initialised, use the “malloc” command. When the memory that has been allocated has to be initialised to zero or a certain value, use the “calloc” command.
33. Describe the distinction between “int* const ptr” and “const int* ptr” in C.
Ans. As a pointer to a constant integer, “const int* ptr” specifies that the value indicated by ptr cannot be changed. On the other hand, “int* const ptr” defines a constant pointer to an integer, meaning the value it points to can be changed but the pointer itself cannot be changed to reference a new memory address.
Summary
The best preparation for tomorrow is doing your best today.
A mentor can show you the path, now it’s your turn to walk on it. These C interview questions helped you to get all kind of question, basic to advanced. Refer our next C Programming Interview Question level – II for more tricky questions.
Revise all the concepts of C programming with DataFlair C Tutorials Series
Hope, you like these C Interview Questions. If you have any suggestion or feedback, feel free to share with us!
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google
q-12 is wrong. Please DO NOT include questions like these !!
new operator are only present in c++.
I got rejected from the interview because of this question.
Great questions !
Great thank you sir… But confused about question 26… i guess it should be 100, then post incremented, 101, pre incremented then 102..may be i am wrong