C Top 50 Interview Questions and Answers

1. What are the main features of the C programming language?

  • Simple and efficient
  • Structured language
  • Low-level access to memory
  • Extensible

2. What is the difference between ++i and i++?

  • ++i is the prefix increment operator; it increments i before its value is used in the expression.
  • i++ is the postfix increment operator; it increments i after its value is used in the expression.

3. Explain the difference between malloc() and calloc().

  • malloc(size_t size) allocates a block of memory of the specified size but does not initialize it.
  • calloc(size_t nitems, size_t size) allocates memory for an array of nitems elements of size bytes each and initializes all bytes to zero.

4. What is a pointer and how is it declared in C?

  • A pointer is a variable that stores the address of another variable.
  • It is declared using the * operator. For example, int *ptr; declares a pointer to an integer.

5. What is a segmentation fault?

  • A segmentation fault occurs when a program tries to access a memory location that it is not allowed to access. This often happens due to dereferencing a null or uninitialized pointer, or accessing memory out of bounds.

6. What is the difference between struct and union?

  • struct allocates memory for all its members separately, allowing them to be accessed independently.
  • union allocates a single shared memory space for all its members, so only one member can contain a value at any time.

7. Explain the difference between == and = operators.

  • == is the equality operator, used to compare two values.
  • = is the assignment operator, used to assign a value to a variable.

8. What is a function pointer and how do you declare one?

  • A function pointer is a pointer that points to the address of a function.
  • It is declared as follows: return_type (*pointer_name)(parameter_list);
    Example: int (*func_ptr)(int, int); declares a pointer to a function that takes two int arguments and returns an int.

9. What is the use of the static keyword in C?

  • In a global variable declaration, it limits the scope of the variable to the file in which it is declared.
  • In a function definition, it makes the function scope local to the file.
  • In a local variable declaration, it retains the variable’s value between function calls.

10. What are the differences between array and pointer in C?

  • An array is a collection of elements of the same type placed in contiguous memory locations.
  • A pointer is a variable that stores the address of another variable.
  • Arrays provide direct access to their elements, while pointers require dereferencing.
  • The name of an array acts as a constant pointer to the first element, while a pointer can be reassigned to point to different locations.

Related articles