C Programming Questions: Know Them Before Your Job Interview

c programming questionsIf you’re a C language programmer and you’re looking to land a well paying job in an IT company, you need to make sure you have the basics of C down pat before you go for the interview. In today’s competitive world, most companies are looking for talented programmers who have a thorough understanding of their chosen area of expertise – whether it’s C programming, C++ coding or Java applications – and can write moderately complex programs without help or supervision.

You have to showcase your programming skills to the interviewer, many times on paper, and answer several tricky questions to stand out from the crowd. They will also take your past programming experience into account (if you have any), but ultimately the way you answer their questions  during the interview will be the deciding factor for your chances of getting the job.

We’ve prepared a list of questions, with brief answers, that a C language programmer with a few years of programming experience would be asked at a typical interview (keep in mind that the actual questions that you are asked may be drastically different):

What is C Language? How is it relevant today?

This question is almost a given in all your interviews! C language, if you didn’t know, was developed in the 1970s for the UNIX operating system by Dennis Ritchie and Ken Thompson. It is a standard program language that was (and still is) wildly popular for a large range of applications, especially in embedded systems.

C language is relevant today because it is the mother language of all programming languages – almost all modern programming languages are based on C. A solid base in this language enables you to grasp other newer languages faster. It is also one of the least complicated and the most powerful languages around as well, which is why many companies prefer it to the newer languages like C++ and Java.  Also, at runtime, programs coded in C run much faster than programs coded in C++.

What is Fibonacci Series? /Write a Program to Print the Fibonacci Series

This is a common question, both in examinations and in interviews. The Fibonacci series is a mathematical series that starts with 0 and 1. Then each subsequent number in the series is the addition of the two numbers that come before that number. For example:

0, 1, 1, 2, 3, 5, 8, 13, ....

The Fibonacci Series has practical applications in computer algorithms for making Fibonacci based data structures, for making graphs and for connecting distributed and parallel systems. The Fibonacci series can also be observed in nature (arrangement of leafs and blooming flowers). Here’s a sample program to generate the series

#include<stdio.h>

int main()
{
   int n=0, x1= 0, x2= 1, next=0, i;

   printf("Enter the number of terms\n");
   scanf("%d",&n);

   printf("First %d terms of the Fibonacci series are :-\n",n);

   for (i=0 ; i < n ; i++ )
   {
      if ( i <= 1 )
         next = i;
      else
      {
         next = x1 + x2;
         x1 = x2;
         x2 = next;
      }
      printf("%d\n",next);
   }

   return 0;
}

Explain the Difference between C and C++

C is the predecessor of C++. C++ is an Object Oriented, non-procedural language, unlike C, which is a procedural language.  C++ treats all data and functional handling in terms of Objects and their relationships.

What are Storage Classes? What are the Types of Storage Classes?

Storage classes are used to determine the scope and the life time of a function or a variable in the system memory. There are four types of storage classes in C: register, static, auto and extern, out of which auto is considered the default storage class for variables.

What is a Pointer in C? What is a NULL pointer? Can you show with an example.

Pointers are one of the most important concepts in C.  A pointer is used to store the memory address of a variable within the system memory.  It’s a handle to the variable’s memory address. A pointer is itself considered a variable as well.  Pointers give a programmer a great deal of power and flexibility within a program. They are typically used to indirectly access structures, arrays and functions – which are all core functionalities of the C language. A null pointer, is a pointer that does not yet point to a memory address.

/* declare a pointer to type char */
char *bufptr;
bufptr=NULL;
/* allocate memory on the heap */
bufptr = malloc(1000);
/* check if the allocation was successful */
if (bufptr == NULL)
{
    printf("Memory allocation failed !\n");
}
/* Do some processing here */

/* Free the memory */
free(bufptr);
bufptr = NULL;

What are Structures in C?

Structures are a collection of locally declared variables in C.  Structures are used to group together an assortment of related variables together, which allows for better handling by the programmer. For example, you want to store information about a new bike in the computer. Instead of storing information about its mileage, different parts, top speed under different names, you can use a single name (structure) to group these variables together.

struct bike_details {
char* pmake;
char* pRegistration;
int   mileage;
int   top_speed;
};

What is Nesting in C?

Including one constructed inside another, is nesting. Nesting can be a structure inside another structure, an if-else statement within another, multiple while loops, one inside the other. Continuing our earlier example, if you have a collection of bikes, instead of grouping them separately, you can nest them within a main structure, which you can name “Collection of Bikes”.

How is calloc () different from malloc ()

The main difference between the two is that calloc (), when it is used to assign a block of memory, the allocated contents are initialized to 0. Malloc (), on the other hand, does not initialise the memory block it assigns. The memory just has random values left over from previous usage.

What are the differences between printf () and scanf ()

This is a very basic question, but one you may be asked by the interviewer to just check your basics. Printf() is used to print (or display) the value of a variable on the screen, while scanf() is used to accept the value of a variable.  Refer our earlier program on the Fibionacci series for an example.

During the interview, you may also be given a few standard programs and be asked to find errors or gaps in them. The only way to do that well, is to make sure you’ve mastered the C language and practised enough coding!

Don’t forget to be calm and confident during your interview, after all, you want to create an awesome impression!