C Interview Questions to ace that programming interview

interviewquestionsoncSo you have a big day coming up. You’ve landed an interview with a top technology company, you know, the one you’ve been dreaming about and been praying to get into. You’ve got butterflies in your stomach. Whether you’re a novice or a pro, you still need to prep for that interview.

Having been on both sides of the seat numerous times over the last decade or so – interviewer and interviewee – I’ve seen enough interviews to know the pattern. Normally the C programming part of the interview begins with questions about the C language, theory and constructs, to get a feel of how well the candidate knows the subject matter. This is followed by some amount of ‘reading’ code and then actually writing programs.

In this article, I will walk you through the typical C theory questions. We will take the actual programming questions separately. Ready to start? Here we go.

If you’re a novice you’re probably wondering how best to present your skills and cover up your lack of substantial programming experience. Some good hands on C programming will definitely help.

If you already have significant programming experience, chances are you write code almost on auto-pilot. You know your stuff. But do you remember the theory? Will you be able to explain it in the interview? It always helps to jog your memory a bit and recollect the buzz words with a good refresher C Programming Course.

What is a null pointer?

It is a pointer that does not point to anything ie any memory address. It is defined as “NULL” and has an internal value that is guaranteed to be unique and different from any valid pointer. It is usually used as a placeholder until you assign an actual value to the pointer and to cross check against valid pointers.

Are you aware of any ways to reduce the size of the executables that are created?

If there are multiple libraries being included, we could try dynamically linking them.

What is a static variable? When should it be used?

A variable declared as static will continue to exist even after the block in which it was defined terminates. There are two main uses of the static variable:

  •  To retain a value between function calls.
  •  To limit scope of a function or global variable to a particular file

Can a variable be both constant and volatile?

Though it seems like a contradiction, it is possible. The const modifier means that this block of code cannot change the variable, but it can be changed by means outside this code.

What is a macro? Is it better to use a macro or a function?

A macro is a pre-processor directive that is expanded at compile time into inline code. This is commonly used to define certain limits or variables or code snippets that are used often. The  advantages of using a macro are that i) It is type – neutral  ii) There is no overhead as in function calls. However macros can be used for only simple pieces of code, since the ‘text’ of the macro is placed inline verbatim.

Do you know what an enum is? Are macros and enums the same?

Enum is short for enumeration, a variable type in Ansi C.  Instead of using int to represent a group of values, an enumeration can be used to map them to more meaningful names. For handling a set of values, enums are a better choice than macros, though it can be a personal choice. Some reasons why enums may be prefered are because they are type defined and  signed. This means they work really well when you have a group of tightly associated sequential integers.

Are you aware of the memory copy (memcpy) and string copy (strcpy) functions? Can they be used interchangeably?

A memory copy is designed to work with any kind of data. You have to explicitly list the number of bytes to be copied. A string copy on the other hand, is a specialised kind of memory copy. It is meant only to copy strings. It copies each byte of the source to the destination, until it reaches the terminating null character.

Is it possible to execute code even after the program exits?

The standard C library has an atexit() function that can be used to execute clean up operations when your program terminates. You can use this to do any operation you want by passing it the function pointers you want.

What is the difference between declaring a variable and defining a variable?

Declaring a variable just tells the compiler its type. No memory is allocated for it yet.  On defining a variable it allocates space to store the variable as well. You can also initialize the variable while declaring it.

 What’s a pointer versus an address?

An address is the place in memory where a variable is stored. A pointer is a variable which stores the address.

How are parameters passed to a function?

Other than for arrays and functions, C passes arguments or parameters by value. That means it creates a local copy of the parameter value within the function. The function cannot modify the actual arguments passed to it.

Is x++ any different from x+1 ?

Yes and no. They both give the same output. Just that x++ can directly be mapped to the INR or increment machine instruction while x+1 will map to a fetch and add and hence require more instructions.

That was a lot of C questions! By now you would have convinced them that you are a pro!

If you missed out on any of the answers, no sweat. It is okay to say you do not know one of the answers. You are not going to be judged harshly just for missing out one answer. Keep your cool and move on to the next one. The interview is more likely to take a holistic view of your attitude and your aptitude. It is quite easy to brush up your programming skills with a quick C course.

Though in this post we have focus on the C questions, to really prepare well for your interview, you should also consider all other aspects.

All the best! Go ace that interview!