C interview questions with answers

 


C interview belongs to specific and targeted interviews. Although you can expect to get some general interview questions, you need to be prepared for specific c interview questions. In this article, we try to summarize 10 most common c interview questions you can get and give you a good answer.

However, as you know how broad the C is, you can expect many variations of these questions, as well as many other IT interview questions. We hope the given list will help you anyway, at least to understand what interviewers generally ask in the interviews about c.

understanding a binar structure is a must if you want to answer the c interview questions

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. Defaultly 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.

And what to say to malloc(…) when answering c interview questions?

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.

 

Exactly what static variable means? (This is one of the most basic c interview questions)

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

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. And lastly, 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.

Can you declare static variables within the header file?

c the top programming languageYou 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.

Check also: Java interview questions or other IT interview questions.

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

Giving questions like this, recruiters try to find out how deeply you really understand c language.

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))

Without using macros, you can hardly do anything serious…

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

The solution is determined by the specific situation you’re writing 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. Remember this when answering the c interview questions.

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.

Therefore, the decision between using a macro or function is just one of selecting between the tradeoff of quicker program speed vs smaller sized program. Normally, an individual really should use macros to replace little, repeatable code areas, and it is best to use functions for more substantial coding duties that may demand a number of lines of code. In general, many c interview questions touch the problematic of use of function and macro, so rather study it properly.

 

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” );

Have a look at data modeling interview questions also.

 

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. So be careful about such tricky questions.

 

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, this is a good way to start answering this tough one of c interview questions.

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.

 

Preprocessor: what it does for a program ?

The C preprocessor, a thing targeted in various c interview questions, 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.

In case your source code includes any kind of conditional preprocessor directives (such as #if), the preprocessor evaluates the situation and modifies your source code accordingly. Do not underestimate these seemingly easy questions.

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.

women is coding in c languageThe 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. Do not let the recruiters to surprise you with the c interview questions about hashing.

 

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.

To look for the item, you just hash it and look at all of the data whose hash values match that of the data you are searching for. This method significantly reduces the quantity of items you must look at. If your parameters are set up carefully and sufficient storage is available for that hash table, the quantity of comparisons required to find an item can easily be made arbitrarily close to one.

 

Conclusion – it’s not only about interview questions targeting the programming

At the end, do not forget that you will have to handle several typical interview questions also and should be well prepared for it. There are countless proves how not the best coder got the job, just because he had an ability to impress the interviewers That is why you should definitely have a look also at some general job interview preparation tips. Do not underestimate your preparation!