1. Tell me what are bitwise shift operators?

<< - Bitwise Left-Shift
Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2.
Expression: a << b
This expression returns the value of a multiplied by 2 to the power of b.

>> - Bitwise Right-Shift
Bitwise Right-Shift does the opposite, and takes away bits on the right.
Expression: a >> b
This expression returns the value of a divided by 2 to the power of b.

2. What is the use of bit field?

Packing of data in a structured format is allowed by using bit fields. When the memory is a premium, bit fields are extremely useful. For example:

- Picking multiple objects into a machine word : 1 bit flags can be compacted
- Reading external file formats : non-standard file formats could be read in, like 9 bit integers

This type of operations is supported in C language. This is achieved by putting :'bit length' after the variable. Example:

struct packed_struct {
unsigned int var1:1;
unsigned int var2:1;
unsigned int var3:1;
unsigned int var4:1;
unsigned int var5:4;
unsigned int funny_int:9;
} pack;

packed-struct has 6 members: four of 1 bit flags each, and 1 4 bit type and 1 9 bit funny_int.

C packs the bit fields in the structure automatically, as compactly as possible, which provides the maximum length of the field is less than or equal to the integer word length the computer system.

The following points need to be noted while working with bit fields:

The conversion of bit fields is always integer type for computation
Normal types and bit fields could be mixed / combined
Unsigned definitions are important.

3. Explain what is the purpose of "extern" keyword in a function declaration?

The declaration of functions defaults to external linkage. The only other storage class possible for a function is static, which must be specified explicitly. It cannot be applied to a block scope function declaration and results in internal linkage.

4. What is the difference between strcpy() and memcpy() function?

The following are the differences between strcpy() and memcpy():

- memcpy() copies specific number of bytes from source to destinatio in RAM, where as strcpy() copies a constant / string into another string.

- memcpy() works on fixed length of arbitrary data, where as strcpy() works on null-terminated strings and it has no length limitations.

- memcpy() is used to copy the exact amount of data, whereas strcpy() is used of copy variable-length null terminated strings.

5. Explain #pragma statements?

The #pragma Directives are used to turn ON or OFF certain features. They vary from compiler to compiler.
Examples of pragmas are:

#pragma startup // you can use this to execute a function at startup of a program
#pragma exit // you can use this to execute a function at exiting of a program
#pragma warn –rvl // used to suppress return value not used warning
#pragma warn –par // used to suppress parameter not used warning
#pragma warn –rch // used to suppress unreachable code warning

6. Do you know the use of fflush() function?

In ANSI, fflush() [returns 0 if buffer successfully deleted / returns EOF on an error] causes the system to empty the buffer associated with the specified output stream.
It undoes the effect of any ungetc() function if the stream is open for input. The stream remains open after the call.
If stream is NULL, the system flushes all open streams.

However, the system automatically deletes buffers when the stream is closed or even when a program ends normally without closing the stream.

7. Do you know the difference between malloc() and calloc() function?

The following are the differences between malloc() and calloc():

- Byte of memory is allocated by malloc(), whereas block of memory is allocated by calloc().

- malloc() takes a single argument, the size of memory, where as calloc takes two parameters, the number of variables to allocate memory and size of bytes of a single variable

- Memory initialization is not performed by malloc() , whereas memory is initialized by calloc().

- malloc(s) returns a pointer with enough storage with s bytes, where as calloc(n,s) returns a pointer with enough contiguous storage each with s bytes.

8. What is the C Language function prototype?

The basic definition of a function is known as function prototype. The signature of the function is same that of a normal function, except instead of containing code, it always ends with semicolon.

When the compiler makes a single pass over each and every file that is compiled. If a function call is encountered by the compiler, which is not yet been defined, the compiler throws an error.

One of the solution for the above is to restructure the program, in which all the functions appear only before they are called in another function.

Another solution is writing the function prototypes at the beginning of the file, which ensures the C compiler to read and process the function definitions, before there is a change of calling the function. If prototypes are declared, it is convenient and comfortable for the developer to write the code of those functions which are just the needed ones.

9. What is the difference between exit() and _exit() function?

The following are the differences between exit() and _exit() functions:

- io buffers are flushed by exit() and executes some functions those are registered by atexit().

- _exit() ends the process without invoking the functions which are registered by atexit().

10. What are the advantages of using macro in C Language?

In modular programming, using functions is advisable when a certain code is repeated several times in a program. However, everytime a function is called the control gets transferred to that function and then back to the calling function. This consumes a lot of execution time. One way to save this time is by using macros. Macros substitute a function call by the definition of that function. This saves execution time to a great extent.

Download Interview PDF

11. Can you please compare array with pointer?

A pointer is an address location of another variable. It is a value that designates the address or memory location of some other value (usually value of a variable). The value of a variable can be accessed by a pointer which points to that variable. To do so, the reference operator (&) is pre-appended to the variable that holds the value. A pointer can hold any data type, including functions.

12. Do you know NULL pointer?

A null pointer does not point to any object.
NULL and 0 are interchangeable in pointer contexts.Usage of NULL should be considered a gentle reminder that a pointer is involved.
It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required.

13. Tell me with an example the self-referential structure?

A self referential structure is used to create data structures like linked lists, stacks, etc. Following is an example of this kind of structure:

struct struct_name
datatype datatypename;
struct_name * pointer_name;

14. Explain indirection?

If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value. If p is a pointer, the value of p is the address of the object. *p means "apply the indirection operator to p"; its value is the value of the object that p points to. (Some people would read it as "Go indirect on p.")

15. How many levels of pointers have?

The answer depends on what you mean by "levels of pointers." If you mean "How many levels of indirection can you have in a single declaration?" the answer is "At least 12."

16. For what purpose null pointer used?

The null pointer is used in three ways:
► To stop indirection in a recursive data structure.
► As an error value.
► As a sentinel value.

17. Tell me when is a void pointer used?

A void pointer is used for working with raw memory or for passing a pointer to an unspecified type.
Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory.

18. Tell me is NULL always defined as 0(zero)?

NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler can't always tell when a pointer is needed).

19. Can we add pointers together?

No, you can't add pointers together. If you live at 1332 Lakeview Drive, and your neighbor lives at 1364 Lakeview, what's 1332+1364? It's a number, but it doesn't mean anything. If you try to perform this type of calculation with pointers in a C program, your compiler will complain.

20. Tell me when would you use a pointer to a function?

Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, "Part of what I do can be customized. Give me a pointer to a function, and I'll call it when that part of the job needs to be done. That function can do its part for me." This is known as a "callback." It's used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application.

21. Tell me can the size of an array be declared at runtime?

No. In an array declaration, the size must be known at compile time. You can't specify a size that's known only at runtime.