Top C Interview Questions and Answers

Here are top C interview questions,


1. What is the difference between `malloc()` and `calloc()` functions?

`malloc()` function stands for "memory allocation"and is used to allocate a block of memory of a specified size. 

`calloc()` function stands for "contiguous allocation"and it allocates memory for an array of elements, each of a specified size, and initializes all the allocated bytes to zero.

void *malloc(size_t size); 

void *calloc(size_t num_elements, size_t element_size);

 

2. What is a pointer in C?

A pointer is a variable that holds the memory address of another variable. It allows direct access and manipulation of memory locations.

 

3. Explain the difference between `const` and `volatile` qualifiers in C.

The `const` qualifier specifies that a variable's value cannot be changed, while the `volatile` qualifier indicates that the variable may change unexpectedly (e.g., by hardware interrupts) and should not be optimized by the compiler. 

The volatile keyword is essential when dealing with memory-mapped hardware or when interacting with variables that can be modified by other threads or processes asynchronously.

 

4. What is the difference between a local variable and a global variable?

A local variable is declared within a specific block or function and has a limited scope, visible only within that block or function. A global variable is declared outside of any function and can be accessed by any part of the program.

 

5. What is the purpose of the `static` keyword in C?

The `static` keyword in C has multiple uses:

     - When used with a global variable, it limits the variable's scope to the current source file.

     - When used with a local variable, it retains its value between function calls.

     - When used with a function, it limits the function's scope to the current source file.

     - When used with a data members within structs, it creates a single instance of the data member shared among all instances of the struct.

 

6. Explain the concept of recursion in C.

Recursion is the process of a function calling itself. It allows the solution of complex problems by breaking them down into smaller, similar sub-problems.

 

7. What are the bitwise operators in C?

The bitwise operators in C are `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (left shift), and `>>` (right shift). They perform operations on individual bits of integers.

 

8. What is the difference between `strcpy()` and `strncpy()` functions?

`strcpy()` is used to copy a string, including the null-terminating character, to another string. `strncpy()` is used to copy a specified number of characters from one string to another, but it may not null-terminate the destination string if the source string is longer than the specified length.

 

9. What is a structure in C?

A structure in C is a user-defined data type that groups together related variables of different types under a single name. It allows creating complex data structures.

 

10. Explain the difference between `scanf()` and `printf()` functions.

`scanf()` is used to read input from the user, while `printf()` is used to display output on the screen.

 

11. What is the purpose of the `break` statement in C?

The `break` statement is used to exit from a loop or switch statement, terminating its execution.

 

12. What is a function pointer in C?

A function pointer is a variable that stores the address of a function. It allows functions to be passed as arguments, returned from other functions, and dynamically invoked.

 

13. How are arrays and pointers related in C?

In C, arrays and pointers are closely related. An array name can act as a pointer to its first element. Additionally, array elements can be accessed using pointer arithmetic.

 

14. Explain the concept of dynamic memory allocation in C.

Dynamic memory allocation in C is done using functions like `malloc()`, `calloc()`, and `realloc()`. It allows the allocation and deallocation of memory during program execution.

 

15. What is a macro in C?

A macro in C is a preprocessor directive that defines a symbolic name or a constant value. It is often used to define functions-like macros or conditional compilation.

 

16. What is the purpose of the `typedef` keyword in C?

The `typedef` keyword is used to create aliases or alternative names for existing data types. It allows defining user-defined types for better code readability.

 

17. Explain the difference between `union` and `structure` in C.

In C, a structure allows storing multiple variables of different data types under a single name, while a union allows overlapping storage of variables. However, only one member of a union can be accessed at a time.

 

18. What are the storage classes in C?

The storage classes in C are `auto`, `register`, `static`, and `extern`. They determine the scope, visibility, and lifetime of variables.

 

19. What is the role of the preprocessor in C?

The preprocessor in C handles preprocessing directives before the compilation process. It performs tasks such as macro substitution, file inclusion, and conditional compilation.

 

20. Explain the difference between `signed` and `unsigned` data types in C.

`signed` data types in C can represent both positive and negative numbers, while `unsigned` data types can represent only non-negative numbers, widening the range of positive values but eliminating negative values.


Above are few top C interview questions. Remember to prepare and expand on these answers.

Good luck with your interview!  👍

Post a Comment

0 Comments