IT companies demand clear understanding and thorough sense of programming logic when it comes to selecting their employees, from a batch of freshers. C being the basic programming language, is targeted as a topic of interview questions when it comes to testing the knowledge of the candidates. Listed below are some basic C Interview Question that can be asked in an interview.
State the different types of storage classes in C? What are they used for?
There are 3 types of storage in C:
Automatic Storage, Static Storage and Allocated Storage.
The Automatic Storage is for variables having block scope without static specifier.
The Static storage is for variables with block scope and having a static specifier.
Allocated storage is for global variables that may or may not have static specifier. The memory obtained in this type of storage is called by malloc(), alloc() and realloc().
What is a null pointer? State how NULL is used?
In C language, it is sometimes mandatory to define pointers that does not point to any memory location. Thus in this case the macro NULL pointer is used. When NULL pointer is defined it gives a literal ‘zero’ value to any data type pointer like integer* or char*.
The NULL pointer is used in the following ways:
To prevent misdirecting in a recursive data structures.
Used as an error value.
Also NULL can be a sentinel value.
What is the difference between calloc() and malloc()?
calloc() is used to allocate memory locations for array elements of predefined size. Its default initialization is 0.
malloc() accepts only memory locations defined in bytes.
Thus to summarize, calloc() allocates blocks of memory and malloc() allocates bytes of memory.
What is the difference between printf() and sprintf()?
sprintf(): is a keyword used to write data to a character array.
printf(): is a keyword used to write data to a standard output device.
Can you declare a static variable in a header file?
A static variable can be declared in a header file. This is will not generate any compilation or runtime error but it will be a logical error if the user defines a static variable in a header file. This is because the header file will now have its own private copy of the variable, which is not logically correct when it comes to programming.
What output will be generated if the statement printf(“%d”) is executed?
This statement is the default print statement of C. Since the statement does not have any variable(of any data type) after “%d”, a garbage value, automatically generated will be shown in the compiler window.
For more information on interview tips and preparation, visit Crack Job Interview