200+ Interview Questions for C Programming – 2024

28 Min Read
Interview Questions in C Programming

You might think of C interview questions to be way more difficult compared to your general exam question papers. Yes, C interview questions are not easy to answer, but they are not that difficult too. Your confidence to answer such interview questions correctly depends on your knowledge level related to the question, and your ability to cope up with and analyze a new problem.

In this article, we will go through some commonly asked as well as interesting questions along with their answers to help students prepare for C interviews.

Attempts have been made to cover all parts of the language in the C interview questions presented in this post. I have included relevant, interesting and common questions/problems from C basics, operators, functions, arrays, pointers, data structures, and more. To provide students a clear concept of the problems, I have included source codes, additional explanations as well as images in some questions.

The most commonly asked or frequently asked C interview questions are like What is…..What is the difference between….Write a C program to…..Find the error in the source code given below….What is the output of the source code given below….etc. I have tried to include different types of questions from different chapters/topics of the C programming language.

The C interview questions here have been divided into 3 parts – frequently asked, interesting questions and other most common C questions. I have provided complete answers along with source codes in the first two parts; I have left the third part with questions only for your own practice.

200+ Frequently Asked C Interview Questions & Answers:

Q1. Mention the different storage classes in C.

This might be one of the most debated C interview questions; the answer to this question varies book by book, and site by site on the internet. Here, I would like to make it clear there are only two storage classes in C, and the rest are storage class specifiers.

As per reference manual of “The C Programming Language” by: Brian W. Kernighan and Dennis M. Ritchie, in the Appendix A of the reference manual, the very first line says: There are two storage classes: automatic and static.
[The C programing Language 2nd Edition,Brian W. Kernighan ,Dennis M. Ritchie ]

Q2. What are the different storage class specifiers in C?

There are 4 storage class specifiers in C, out of which auto and static act as storage classes as well. So, the storage class specifiers are:

  • Auto
  • Register
  • Static
  • Extern

Q3. What are library functions in C?

Library functions are the predefined functions in C, stored in .lib files.

Q4. Where are the auto variables stored?

Auto variables are stored in the main memory. The default value of auto variables is garbage value.

Q5. What is the difference between i++ and ++i?

One of the most commonly asked C interview questions or viva questions – i++ and ++i. The expression i++ returns the old value and then increases i by 1, whereas the expression ++i first increases the value of i by 1 and then returns the new value.

Q6. What is l-value in C? Mention its types.

Location value, commonly known as the l-value, refers to an expression used on the left side of an assignment operator. For example: in the expression “x = 5”, x is the l-value and 5 is the r-value.

There are two types of l-value in C – modifiable l-value and non-modifiable l-value. modifiable l-value denoted a l-value which can be modified. non-modifiable l-value denote a l-value which cannot be modified. const variables are non-modifiable l-value.

Q7. Can i++ and ++i be used as l-value in C?

In C both i++ and ++i cannot be used as l-value. Whereas in C++, ++i can be used as l-value, but i++ cannot be.

Q8. Which of the following shows the correct hierarchy of arithmetic operations in C?

(1) / + * –
(2) * – / +
(3) + – / *
(4) * / + –

4 is the correct answer.

Q9. Which bit wise operator is suitable for

  1. checking whether a particular bit is on or off?
    Ans. The bitwise AND operator.
  2. turning off a particular bit in a number?
    Ans. The bitwise AND operator.
  3. putting on a particular bit in a number?
    Ans. The bitwise OR operator.

Q10. Can a C program be written without using the main function?

This is one of the most interesting C interview questions. I guess, up until now, you might not have ever written a C program without using the main function. But, a program can be executed without the main function. See the example below:

#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
    printf(” hello “);
}

Now, lets see what’s happening within the source code. Here, #define acts as the main function to some extent. We are basically using #define as a preprocessor directive to give an impression that the source code executes without the main function.

Q11. What is the output of printf(“%d”); ?

For printf(“%d”, a); the compiler will print the corresponding value of a. But in this case, there is nothing after %d, so the compiler will show garbage value in output screen.

Q12. What is the difference between printf() and sprintf() ?

printf() statement writes data to the standard output device, whereas sprintf() writes data to the character array.

Q13. What is the difference between %d and %*d ?

Here, %d gives the original value of the variable, whereas %*d gives the address of the variable due to the use of pointer.

Q14. Which function – gets() or fgets(), is safe to use, and why?

Neither gets() nor fgets() is completely safe to use. When compared, fgets() is safe to use than gets() because with fgest() a maximum input length can be specified.

Q15. Write a C program to print “Programming is Fun” without using semicolon (;) .

Here’s a typical source code to print “Programming is Fun”.

#include<stdio.h>
int main()
{
      printf("Programming is Fun");
      return 0;
}

There’s not much trick in printing the line without using the semicolon. Simply use the printf statement inside the if condition as shown below.

#include<stdio.h>
int main()
{
      if( printf( "Programming is Fun" ) )
      {    }
}

An extension of the above can be – write a C program to print “;” without using a semicolon.

#include<stdio.h>
int main()
{
   if(printf("%c",59))
   {
   }
}

Q16. What is the difference between pass by value and pass by reference?

This is another very important topic in this series of C interview questions. I will try to explain this in detail with source code and output.

Pass by Value:

This is the process of calling a function in which actual value of arguments are passed to call a function. Here, the values of actual arguments are copied to formal arguments, and as a result, the values of arguments in the calling function are unchanged even though they are changed in the called function. So, passing by value to function is restricted to one way transfer of information. The following example illustrates the mechanism of passing by value to function.

#include<stdio.h>
void swap(int a, int b)
{
    int temp ;
    temp=a;
    a=b;
    b=temp;
}
main()
{
    int x, y,sum;
prinft(“Enter x and y : );
    scanf(“%d%d”,&x,&y);
    printf(“ Before swap, x=%d, y=%d”, x,y)
    swap(x,y);
    printf(“After swap, x=%d, y=%d”, x,y)
}

//Output:
Enter x and y: 5  10
Before swap, x=5, y=10
After swap, x=5, y=10//

In this example, the values of x and y have been passed into the function and they have been swapped in the called function without any change in the calling function.

Pass by Reference:

In pass by reference, a function is called by passing the address of arguments instead of passing the actual value. In order to pass the address of an argument, it must be defined as a pointer. The following example illustrates the use of pass by reference.

#include<stdio.h>
void swap(int a*, int b*);
main()
{
    int x, y,sum;
prinft(“Enter x and y : );
    scanf(“%d%d”,&x,&y);
    printf(“ Before swap, x=%d, y=%d”, x,y)
    swap(&x, &y);
    printf(“After swap, x=%d, y=%d”, x,y)
}
void swap(int *a, int *b)
{
    int temp ;
    temp=*a;
    *a=*b;
    *b=temp;
}

//Output:
Enter x and y: 5  10
Before swap, x=5, y=10
After swap, x=10, y=5//

In this example, addresses of x and y have been passed into the function, and their values are swapped in the called function. As a result of this, the values are swapped in calling function also.

Q17. Write a C program to swap two variables without using a third variable.

This is one of the very common C interview questions. It can be solved in a total of five steps. For this, I have considered two variables as a and b, such that a = 5 and b = 10.

#include<stdio.h>
int main(){
    int a=5,b=10;

    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);

    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    return 0;
}

Q18. When is a switch statement better than multiple if statements?

When two or more than two conditional expressions are based on a single variable of numeric type, a switch statement is generally preferred over multiple if statements.

Q19. Write a C program to print numbers from 1 to n without using loop.

void printNos(unsigned int n)
{
  if(n > 0)
  {
    printNos(n-1);
    printf("%d ",  n);
  }
}

Q20. What is the difference between declaration and definition of a function?

Declaration of a function in the source code simply indicates that the function is present somewhere in the program, but memory is not allocated for it. Declaration of a function helps the program understand the following:

  • what are the arguments to that function
  • their data types
  • the order of arguments in that function
  • the return type of the function

Definition of a function, on the other hand, acts like a super set of declaration. It not only takes up the role of declaration, but also allocates memory for that function.

Q21. What are static functions? What is their use in C?

In the C programming language, all functions are global by default. Therefore, to make a function static, the “static” keyword is used before the function. Unlike the global functions, access to static functions in C is restricted to the file where they are declared.

The use of static functions in C are:

  • to make a function static
  • to restrict access to functions
  • to reuse the same function name in other file

Q22. Mention the advantages and disadvantages of arrays in C.

Advantages:

  • Each element of array can be easily accessed.
  • Array elements are stored in continuous memory location.
  • With the use of arrays, too many variables need not be declared.
  • Arrays have a wide application in data structure.

Disadvantages:

  • Arrays store only similar type of data.
  • Arrays occupy and waste memory space.
  • The size of arrays cannot be changed at the run time.

Q23. What are array pointers in C?

Array whose content is the address of another variable is known as array pointer. Here’s an example illustrating array pointers in C.

int main()
{
    float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]= {&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;
}

Q24. How will you differentiate  char const* p and const char* p ?

In char const* p, the pointer ‘p’ is constant, but not the character referenced by it. Here, you cannot make ‘p’ refer to any other location, but you can change the value of the char pointed by ‘p’.

In const char* p, the character pointed by ‘p’ is constant. Here, unlike the upper case, you cannot change the value of character pointed by ‘p’, but you can make ‘p’ refer to any other location.

Q25. What is the size of void pointer in C?

Whether it’s char pointer, function pointer, null pointer, double pointer, void pointer or any other, the size of any type of pointer in C is of two byte. In C, the size of any pointer type is independent of data type.

Q26. What is the difference between dangling pointer and wild pointer?

Both these pointers don’t point to a particular valid location.

A pointer is called dangling if it was pointing to a valid location earlier, but now the location is invalid. This happens when a pointer is pointing at the memory address of a variable, but afterwards some variable has been deleted from that particular memory location, while the pointer is still pointing at that memory location. Such problems are commonly called dangling pointer problem and the output is garbage value.

Wild pointer too doesn’t point to any particular memory location. A pointer is called wild if it is not initialized at all. The output in this case is any address.

Q27. What is the difference between wild pointer and null pointer?

Wild pointer is such a pointer which doesn’t point to any specific memory location as it is not initialized at all. Whereas, null pointer is the one that points the base address of segment. Literally, null pointers point at nothing.

Q28. What is far pointer in C?

Far pointer is the pointer that can access all the 16 segments, i.e. the whole residence memory of RAM. The size of far pointer is 4 byte. The example below illustrates the size of far pointer in C.

int main()
{
    int x=10;
    int far *ptr;
    ptr=&x;
    printf("%d",sizeof ptr);
    return 0;
}
//Output: 4

Q29. What is FILE?

FILE is simply a predefined data type defined in stdio.h file.

Q30. What are the differences between Structure and Unions?

  1. Every member in structure has its own memory, whereas the members in a union share the same member space.
  2. Initialization of all the member at the same time is possible in structures but not in unions.
  3. For the same type of member, a structure requires more space than a union.
  4. Different interpretations of the same memory space is possible in union but not in structures.

Q31. Write a C program to find the size of structure without using sizeof operator?

struct  ABC
{
    int a;
    float b;
    char c;
};
int main()
{
    struct ABC *ptr=(struct ABC *)0;
    ptr++;
    printf("Size of structure is: %d",*ptr);
    return 0;
}

Q32. What is the difference between .com program and .exe program?

Both these programs are executable programs, and all drivers are .com programs.

  • .com program executes faster than .exe program.
  • .com file has higher preference than .exe type.

Some Interesting C Interview Questions:

1. Why does n++ execute faster than n+1 ?

The execution depends on the amount machine instruction to carry out the operation in the expression. n++ requires only a single machine instruction, such as INR, to carry out the increment operation. On the other hand, n+1 requires more machine instructions to carry out this operation. Hence, n++ executes faster than n+1 in C.

2. Is int x; a declaration or a definition? What about int x = 3; and x = 3; ?

int x; is a declaration. int x = 3; is a declaration with initialization/definition of variable x with a value. x = 3; is a definition.

3. What is the parent of the main() function?

The parent of main() function is header files – the <stdio.h> header file.

4. Write a C program to print the next prime number for any number entered by the user.

#include< stdio.h>
#include< conio.h>
void main()
{
    int i,j=2,num;
    clrscr();
    printf("Enter any number: ");
    scanf("%d",&num);
    printf("Next Prime number: ");
    for(i=num+1; i< 3000; i++)
    {
        for(j=2; j< i; j++)
        {
            if(i %j==0)
            {
                break;
            } // if
        } // for
        if(i==j || i==1)
        {
            printf("%d\t",i);
            break;
        }// if
    }// outer for getch();
}

5. Write a C program to solve the “Tower of Hanoi” question without using recursion.

Try this yourself.

6. Create an array of char type whose size is not known until its run, i.e., the size must be given by the user.

Hints are given in the source code below. If you want to use array finish, you must free it to return resources to memory.

char *sz = NULL;
int i = 0; 
scanf("%d", i );
if(i){ sz = (char *)malloc( sizeof(char)*i);

Other Commonly Asked C Interview Questions:

These are some other commonly asked C interview questions or exam/viva questions. Try answering these on your own, and if you need help with any, mention them in the comments section at the end.

  1. What do you understand by Operator, Operand and Expression in C?
    Operands are the items that are operated on by the operators. In other words, it is the data that is used for calculation. Operators are the ones that perform the operation. Here are the meanings of the 3 terms.Operator – Operator means the operator or the function that is being performed by the operator. So, it is the one that performs the operation.Operand – An operand is the thing that is being operated upon by the operator.

    Expression – An expression is the combination of operands and operators.

  2. What is the difference between declaration and definition of a variable?
  3. Can static variables be declared in a header file?
  4. Can a variable be both constant and volatile?
  5. What are C identifiers?
  6. Can include files in C be nested?
  7. What are flag values?
  8. Write the prototype of printf function.
  9. What is the use of void data type in C?
  10. What is the use of typedef in C?
  11. Differentiate for loop and while loop.
  12. Why is the main() function used in C?
  13. What are macros? Mention their advantages and disadvantages.
  14. Mention the advantages of a macro over a function.
  15. What is function recursion in C?
  16. What keyword is used to rename a function in C?
  17. What is the difference between Calloc() and Malloc() ?
  18. What is the difference between strings and character arrays?
  19. What are the differences between sizeof operator and strlen function?
  20. Differentiate between static memory allocation and dynamic memory allocation.
  21. What are the uses of pointers in C?
  22. What is a null pointer assignment error?
  23. What are enumerations?
  24. Mention the advantages of using unions in C.
  25. Differentiate a linker and linkage.
  26. What is the difference between C++ and C programming?
  27. What are the advantages of C programming?
  28. What are the disadvantages of C programming?
  29. Give an example of error handling in C programming.
  30. How do you write a program in C?
  31. What is the difference between C and C++ programming?
  32. What is the use of C++?
  33. Give an example of polymorphism in C++.
  34. What is the difference between object-oriented and procedural programming?
  35. What are the different memory models?
  36. What is the difference between static and dynamic linking?
  37. Explain the use of pointer.
  38. What is the difference between a variable and a constant?
  39. Explain the scope of variables.
  40. What is a statement?
  41. Explain the difference between goto and break statements?
  42. What is the difference between the forward and backward jump statements?
  43. How do you declare an integer variable?
  44. Define what a loop is?
  45. What is the difference between while and for loops?
  46. What is the difference between a conditional and a loop?
  47. Define what an array is?
  48. Explain the use of malloc and free functions.
  49. What are the types of data structures?
  50. What are the different data types in C?
  51. What is the difference between a data structure and a variable?
  52. What is the difference between a variable and a constant?
  53. What is the difference between a character and a string?
  54. Define the difference between a pointer and an array.
  55. Define the difference between a linked list and a queue.
  56. Define the difference between a stack and a heap.
  57. Define the difference between a stack and a queue.
  58. Define the difference between a linked list and a queue.
  59. What is the difference between char and character?
  60. What is the difference between signed and unsigned integer types?
  61. What is the difference between byte and word types?
  62. What is the difference between pointer and array types?
  63. What is the difference between float, double, and long integer types?
  64. How to declare and use variables?
  65. What is the difference between integer and real types?
  66. What is the difference between structure and union types?
  67. What is the difference between arrays and pointers?
  68. What is the difference between functions and subroutines?
  69. What are the return values of functions?
  70. What is the difference between switch and if statement?
  71. What are the differences between for loop and while loop?
  72. What is the difference between goto and break statements?
  73. What is the difference between enum and typedef?
  74. What are the differences between const and volatile?
  75. What is the difference between inline and static?
  76. What is the difference between static and volatile?
  77. What is the difference between static and extern?
  78. What is the difference between extern and static?
  79. What is the difference between static and global?
  80. What are the differences between typedef, enum, union, and struct?
  81. What is the difference between char, int, float, and long?
  82. What is the difference between void and int?
  83. What is the difference between void and char?
  84. What is the difference between char, int, float, and long?
  85. What are the differences between union and struct types?
  86. What are the differences between function and procedure types?
  87. What is the difference between void and return types?
  88. What is the difference between class and interface types?
  89. What is the difference between pointers and arrays?
  90. What is the difference between arrays and references?
  91.  What is the difference between arrays and pointers?

So, what do you think of the C interview questions mentioned here? Did you find them new, difficult, interesting or boring: we would like to hear your feedback. Also, you can suggest us more interview questions that would suit this post.

Share This Article
8 Comments
  • Nice article,
    I was searching for something like this i was browsing the net since ours but dint find exact question and answers which justifies the question your article is the best and have all the important questions include in it.

    • #include

      int main()
      {
      int a, b;
      scanf(“%d%d”,&a,&b);
      a=a+b;
      b=a-b;
      a=a-b;
      printf(“\n After swap a=%d b=%d”,a,b);
      return 0;
      }

    • I sort of get this question as — how does a source code become a program? Are you asking how does a source code get converted into object code?

      If yes, the answer is compiler. Source code is turned into object code by a compiler. Then, once the object code is passed through a linker, an executable program comes into existence.

Leave a Reply

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

English
Exit mobile version