In this article, we will show you 10 common technical interview questions that will test your knowledge of C, and we will also suggest a good answer to each questions. Let’s start!

 

Q1: What exactly will be the difference between calloc() and malloc() ?

1. calloc(…) allocates the block of memory space for the array of elements of a particular size. By default this block will be initialized to 0. A total number of memory allocated is going to be (number_of_elements * size).

malloc(…) takes in just a sole argument, the memory needed in bytes. malloc(…) allocated bytes of memory space and not blocks of memory like calloc(…) would allocate.

2. malloc(…) always allocates memory blocks and returns the void pointer to the allocated space, or NULL should there be not enough memory available.

calloc(…) allocates an array in memory with elements initialized to 0 and returns the pointer to the particular allocated space. calloc(…) calls malloc(…) to be able to make use of the C++ _set_new_mode function to set the latest handler mode.

 

Q2: Exactly what static variable means?

You can find three principal uses for the static variable. This is a good way how to start your answer to this question. Let’s look at them:

1. Firstly, when you declare inside of a function: This retains the value between function calls

2. Secondly, when it is declared for the function name: By default function is extern. Therefore it will be visible out of different files if the function declaration is set as static. It will be invisible for outer files.

3. Static for global parameters: By default you can use global variables from outside files. When it’s static global, this variable will be limited to within the file.

 

Q3: Can you declare static variables within the header file?

You actually can not declare the static variable if it is not defined at the same time (simply because the storage class modifiers extern and static are exclusive, mutually). The static variable could be defined within a header file, however this would result in each and every source file which included header file to have its very own private duplicate from the variable, and this probably was not your intention. Be careful, these kind of c interview questions are quite tricky and many applicants get caught answering them.

Note: To get a job of a programmer, you will have to answer also some tough behavioral questions, and convince the interviewers of your motivation, and right attitude to various work-related situations. You can check these questions here: behavioral interview questions.

Funny picture that says that C is God's programming language. We can see big letter C on the picture

Q4: Are you able to name any sort of advantages of the macro over the function?

Macro actually gets to see compilation environment, therefore it is able to expand __ __TIME__ __FILE__ #defines. It’s expanded through the preprocessor.

For instance, coder can’t do the following without using macros:

#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR)

PRINT( 11+9*4 ) // expands into printf(”11+9*4=%d”, 11+9*4 );

You are also able to specify your own mini language by using macros:

#define strequal(A,B) (!strcmp(A,B))

Special Tip: Download the full list of questions in a simple, one-page long PDF, and practice your interview answers anytime later, even when offline:

list of questions, PDF

 

Q5: In general, what is better to use: a function or a macro?

The answer depends on the specific situation you’re writing your code for. Macros hold the distinctive advantage of being more effective (as well as faster) compared to functions, since their corresponding code is placed straight into the source code at the position where the macro is called.

There’s no overhead associated with using a macro like there’s in placing the call to some function. Nevertheless, macros are in general tiny and can not tackle huge, complex coding constructions.

The function is far more suited to this sort of situation. In addition, macros are expanded inline, meaning that the code will be replicated for every existence of the macro. Your code thus could possibly be fairly larger if you use macros compared to situation when you were to use functions.

 

Q6: Can you name a suitable bit wise operator for checking whether a certain bit is on or off ?

The bitwise AND operator. Let’s have a look at the following example:

enum {KBit0 = 1, KBit1,…KBit31,}; if ( some_int & KBit24 ) prin tf ( “Bit number 24 is ON\n” );else printf ( “Bit number 24 is OFF\n” );

 

Q7: Is there a way to find out the size of an allocated portion of memory?

There actually isn’t a way, indeed. free() can do it, but there is no way so your program will know the trick free() uses when doing this. Although you may disassemble a library and see the trick, there is absolutely no guarantee the actual trick will not change with following release of the compiler.

 

Q8: Best ways I can convert the string into a number?

The typical C library offers a number of functions intended for converting strings to numbers of just about all formats (longs, floats, integers etc.) and also vice versa, converting numbers to strings.

The next functions may be used to convert strings to numbers or vice-verse:

Format of writing: Function Name/Purpose; atoi()/ Converts a string to an integer. ; strtol()/ Converts a string to a long integer as well as reports any kind of remaining numbers that may not be converted.

atof()/ Converts a string to a double-precision floating-point value. ; atol()/ Converts a string to a lengthy integer. ; strtod()/ Converts the string to a double-precision floating-point value and also reports any leftover numbers which; could not be converted. ; strtoul()/ Converts a string to an unsigned long integer and reports any leftover numbers that could not be converted.

 

Q9: Preprocessor: what it does for a program ?

The C preprocessor, can be used to adjust the program in line with the preprocessor directives within your source code. A preprocessor directive is a statement (such as #define) which gives the preprocessor particular directions regarding how to modify the source code.

The preprocessor is invoked as very first part of your compiler program’s compilation step. In most cases it is hidden from the programmer as it’s run on auto-pilot by the compiler.

The preprocessor reads in all of your included files and also the source code you’re compiling and generates a preprocessed variation of the source code. This particular preprocessed version features each of its macros and also constant symbols replaced by their corresponding code as well as value assignments.

A woman sits at a computer, and looks at the screen, lost in thoughts. We can see exercise book on her table, and she wears glasses

Q10: Can you define hashing?

To hash means to grind up, that’s basically what hashing is about. The core of the hashing algorithm is a hash function which takes your nice, neat data and grinds that in to some random-looking integer.

The concept behind hashing is that some data either doesn’t have inherent ordering (for example pictures) or perhaps is costly to compare (for example photos). In case that data doesn’t have inherent ordering, nobody can execute comparison searches.

In the event the data is expensive for comparison, the quantity of comparisons used even by way of a binary search could be simply too many. Therefore rather than looking at the data on their own, you’ll condense (hash) your data to the integer (its hash value) and retain all the data with the exact same hash value in the identical place. This is performed using the hash value as an index into an array.

 

Success in an interview depends on more than just your technical skills

Most of the people who will compete for the job with you will have a good knowledge of C. You should focus also on other parts of your interview preparation, to ensure that you know how to answer the personal and behavioral interview questions, and how to make a good impression on your interviewers. We suggest you to have a look at the following articles:

  • Screening interview – Typically the first stage of an interview process for programmers. Companies conduct it online, on Skype, over the phone. Most employers will screen out more than fifty percent of job applicants, before inviting the rest for the in-person interviews. The goal of the screening interview is to understand you as a person, your motives and your attitude to work.
  • Most common behavioral interview questions – Inquiring about your past, we try to understand how you would act in your new job, while facing various challenges and tasks. Programmers experience the same situations as any other employee does–conflicts with their colleagues, meeting goals, and failing to meet them, experiencing disappointment, and feeling pressure. Learn how to answer interview questions that deal with these subjects.
  • Work portfolio for an interview – Learn how to prepare a selection of your best works, and how to use it to show the interviewers the value you can bring to their team. Show that that you have already coded something, and that your works helped someone.
Antony
Latest posts by Antony (see all)