25+ C Interview Questions and Answer to Shape your Career

Get Certified in C Programming and Take Your Skills to the Next Level

Preparation is the only mantra that will help you to get success. Don’t wait for an opportunity. Create it. Follow this mantra and make every opportunity count with these C interview questions and answers.

Success doesn’t just find you. You have to go out and get it.      By Marva Collins

Here, we will help you in all possible ways. These are some latest C interview questions with answers that will guide you on how to crack and deal with interview questions.

So, let’s dive into the ocean of knowledge.

Refer this link, if you want to practice some tricky Interview Questions for C Programming. 

1. Popular C Interview Questions and Answer

Q.1 How would you print the string “DataFlair” without using a semicolon to terminate the printf() statement?

Ans. You can print a string without terminating the printf() function by a semicolon with the help of if statement:

#include<stdio.h> 
int main() 
{ 
if(printf("DataFlair")) 
{}
return 0; 
}

Output-

terminate the printf() statement in C

Q.2 Consider the following variable declarations:

float f;
long l;
short s;

What would be the data type of the expression: f – l / s

Ans. The resultant expression would be of the float data type in C.

Here is a diagrammatic representation of how it works:

Implicit Type Conversion in C

Q.3 How would you print all the numbers between 60 to 80 without using any loops?

Ans. We can easily print all the numbers in a given range with the help of recursion in C.

Here is a code that would help you solve this problem:

#include <stdio.h>
int main()
{
static int n = 60;
if(n <= 80)
{
printf("%d ", n++);
main();
}
return 0;
}

Output-

Print number with loops in C

Q.4 Why can’t you initialize a data element of a structure inside a structure?

Ans. In a structure, the data elements have no physical meaning. Memory in a structure is allocated only after the variable (object) associated with it is created. The data members of a structure are accessed through variables (objects) and hence initializing a value to a data member inside a structure is absurd.

Do you know Structures in C Makes Coder Life Easy?

Q.5 Sometimes we can enclose the header file within double quotes instead of angular brackets. When can it be done?

Ans. Enclosing the header file name within angular brackets signifies that the header file is located in the standard folder of all other header files in C.

Enclosing the header file name within double quotes signifies that the header file is located in the present folder you are working with. It is a preferred practice to include user-defined header files in this manner.

Q.6 How would you dereference a void pointer if its data type is not known? 

Ans. A void pointer can be dereferenced only by explicit type casting in C.

For instance,

int number = 20;
void *pointer = &number;
printf(“%d\n”, *((int*)pointer));

Q.7 What are the files generated during processing?

Ans. From compilation to running, the source code with extension .c is converted into expanded source file with the .i extension which is then converted into the object code with .obj extension and then finally converted to the executable code with .exe extension.

Q.8 Which has greater time complexity: Iteration or Recursion and Why?

Ans. Recursion has a greater time complexity as the entire function is called repeatedly wears in iteration, the time complexity simply depends on the number of cycles of the loop. Internally, for every recursion, a new stack in the computer memory is created.

Q.9 What is a wild pointer?

Ans. Wild pointers in C are nothing but simply pointers that are left uninitialized. They can prove to be dangerous to use as they point to some random memory address and may cause the computer system to crash.

Q.10 What does %0.3f indicate in C?

Ans. The %0.3f is a modified version of a format specifier to indicate the floating-point value of a variable up to 3 decimal places.

2. Commonly Asked C Interview Questions with Answers

Q.11 How would you assign the value of an integer data type without using the equal to “=” operator?

Ans. The assignment of value to a variable of any data type can be done by enclosing the value within simple brackets followed by the identifier.

In this case, this is how you would assign a value to a variable of int data type:

int value(10);

It is similar to int value = 10;

Q.12 In C, what would be the value of the expression: 4 + 15 % 4 * 7 – 9

Ans. The modulus operation has equal precedence as multiplication. Therefore, the expression simplifies can be simplified in the following manner:

=> 4 + 15 % 4 * 7 – 9
=> 5 + 3 * 7 – 9
=> 4 + 21 – 9
=> 25 – 9
=> 16

The answer is 16.

Enhance Your Fundamental Skills With Operators in C

Q.13 Is it incorrect to use String = “DataFlair” in C? If yes, then what is the correct way to assign a value to a string?

Ans. A value of a string is copied by using the strcpy() function. The correct way is:

strcpy(String, “DataFlair”);

Q.14 What is the difference between gets() and fgets() function?

Ans. Both the functions are used to take the input of a string but they are different in terms of the syntax used.

The syntax of gets() is:

gets(string_name);

The syntax of fgets() is:

fgets(string_name, string_size, stdin);

There is a problem associated with gets() which makes it dangerous to use because of Buffer Overflow as gets() is not capable of performing the array bound test. Hence, gets() is deprecated. To overcome this problem, fgets() is used instead of gets() function in C.

Q.15 How would you preserve the value of a variable that becomes out of scope?

Ans. There are certain situations that the programmer might encounter where the value of a variable gets out of scope. With the help of the “static” keyword, the programmer can preserve the value of such variables.

This basic syntax of using the static keyword is:

static data_type identifier = value;

For instance,

static int number = 1;

Q.16 Write a C program to find the sum of the series up to n terms:

1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) + …

Ans. 

#include<stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int n, sum1, sum2 = 0, i, j;
printf("Enter the number of terms in the series n: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
sum1 = 0;
for(j = 1; j <= i ; j++)
sum1 = sum1 + j;
sum2 = sum2 + sum1;
}
printf("The sum of the series is: %d\n",sum2);
return 0;
}

Output-

Output of a series in C

Q.17 How would you allocate enough memory for an array of 2000 floating-point values?

Ans. This is how we can perform the given task:

float* const value(new float[2000]);

Get Samurai Technique to Learn Arrays in C 

Q.18 What is the difference between the following declarations:
double *pointer = new double(10);
double *pointer = new double int[5];

Ans. In this first declaration, the memory of one double data type is allocated to pointer and it is initialized to 5. It means that the value 10 is stored in the pointer.

In the second declaration, the memory is allocated to 10 contiguous double data type elements in the array. The beginning address is stored in ‘pointer’.

Q.19 What is meant by a dangling pointer?

Ans. A dangling pointer is basically a pointer that points to the memory address of a variable that has been deleted or freed. A dangling pointer is mainly used during the deallocation of memory, function calls, and when the variable goes out of scope.

Q.20 What is the use of the volatile?

Ans. Volatile is basically a keyword that tells the compiler not to optimize anything that can change in ways that can’t be defined by the compiler.

3. Scenario-Based C Interview Questions and Answers

Q.21 When do we require to use the concept of a bit field?

Ans. A bit field in C is basically required to reduce the memory consumption by the variables. It is required when we already know that a particular data type can have values restricted to a particular range.

For example, we know that there are not more 31 days in a month. Therefore, instead of using the int data type to specify the number of days in a month, we can restrict its range of values from 1 to 32.

Q.22 What will happen if you store the value 32768 in an int data type?

Ans. You cannot store 32768 in the int data type as it is out of the range of integral values. The range of int data type is from -32768 to 32767.

Q.23 What is the difference between a pointer to an array and an array of pointers?

Ans. A pointer to an array is basically a pointer that points to the entire array, that is, it simply does not only point to the first element of the array but the complete array itself. It is usually done when dealing with multidimensional arrays in C

This is how it is done:

float (*pointer)[10];

Here, the pointer points to the entire array consisting of 10 floating-point values.

An array of pointers is simply an array that consists of pointers. The pointer in the array points to a memory address of the variable.

For instance,

char *string[ ]={“Data”, “Flair”, “Tutorials”};

Q.24 What is the basic principle behind bucket sort? 

Ans. Bucket sort (also called as radix sort) is based on the principle of sorting data elements (numbers or strings) with integer keys by grouping the keys by their corresponding individual digits that share the same significant position as well as value.

Learn everything about Strings in C

Q.25 What are self-referential structures?

Ans. Self-referential structures are nothing but structures that include a data member which is a pointer to another structure of similar type.

For instance,

struct student
{
int age;
float marks;
student *pointer; // Self-referential pointer 
}

Here, the structure student contains a data member pointer that points to another structure student of similar type.

Q.26 What is the difference between getch() and getche() functions?

Ans. Both functions are used to accept a character as an input value. By using getch(), the key that was pressed will not appear on the screen. It will automatically be captured and be assigned to a variable. On the other hand, by using getche(), the key that was pressed by the user will appear on the screen, while at the same time being assigned to a variable.

Q.27 What logic would you apply to print the left diagonal of any n x n matrix in C?

Ans. This is how indexing of rows and columns look like in a n x n matrix.

nxn Matrix

Here, we observe that the index of the rows and columns of the left diagonal are equal.

Therefore, the base condition would be:

If the rows and columns are represented with i and j respectively, then:

if ( i == j )
{
printf (“ %d ”,a[ i ] [ j ] );
}

Q.28 Predict the output of the following code:

#include<stdio.h>
int main() 
{ 
char array[] = "DataFlair"; 
char *pointer = array; 

while(*pointer != '\0') 
++*pointer++; 
printf("%s\n",array); 
return 0; 
}

Ans. The output would be EbubGmbjs

This problem is solely used on the understanding of postfix, prefix and dereference operators.
The program shows the increment of the value of the string “DataFlair”.

Output-

Output of C program

Q.29 Predict the output of the following code segment:

#include <stdio.h> 
#define fgets "%s DataFlair" 
int main() 
{
printf(fgets, fgets);
return 0; 
}

Ans. %s DataFlair DataFlair

Output-

Result of C program

Q.30 Predict the output of the following code segment:

#include <stdio.h> 
int main() 
{ 
int i = 10; 
short s; 
long l = 3.3;
printf("%ld\n", sizeof(i++ + s*l)); 
return 0; 
}

Output-

Output of C Code

4. Summary

Your limitations are only imaginations. Believe in yourself and achieve everything in your life. These C Interview Questions and Answers will help you get a job in top IT companies like TCS, Infosys, Accenture, Intel, etc.

You can revise all the topics from our Free C Tutorial Series.

Thanks for allowing us to serve you better. Now, hold the steering and drive your career.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *