C Interview Questions and Answers for Freshers

    1 Votes

9) What happens if you free a pointer twice?

It is really dangerous to free the same memory twice. If the memory has not been reallocated in between, it will generate a “double free” error, since the memory location has already been freed.

10) How does free() method know about how much memory to release?

There's no concrete way. Most systems, keeps a track of each memory block as linked lists. When memory is allocated, all the blocks that are given to that particular call are put into a linked list and the size, block number and serial number are written in the head node. There is no assurance, though. But in some way or other, the system keeps track of each block to know the size of each allocated portion of the heap.

11) How to restrict a header file from including more than once?

In C, to avoid double inclusion, we use a include guard also known as macro guard. It is #ifndef - #endif pair. "ifndef" is an indication of “if not defined”.

#ifndef FAMILY_H
#define FAMILY_H
struct Example
{
  int member;
};
#endif /* FAMILY _H */

12) How to print an address?

The best way is to use "%p" in printf() or fprintf. The “%p” will tell compiler to use the best type to use, while printing the address according to the environment, since the size of a pointer changes from system to system.

13) Enter the output of the program

#include <stdio.h>
enum day {sunday = 1,monday,tuesday,wednesday,thursday = 3,friday,saturday};

int main()
{
  int i;
  printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
  wednesday, thursday, friday, saturday);
  scanf("%d",&i);
  return 0;
}

The answer is : 1 2 3 4 3 4 5

Explanation: The enum assigns the value with single increment. If a value is explicitly assigned to an element, it will just set that value to that element and start to increment from that assigned value for the following elements.

14) Explain recursive functions? Also explain the advantages and disadvantages of Recursive algorithms?

A recursive function is a function which calls itself. The advantages of recursive functions are:

  • A substitute for very complex iteration. For example, a recursive function is best to reduce the code size for Tower of Hanoi application.
  • Unnecessary calling of functions can be avoided.

The disadvantages of Recursive functions:

  • The exit point must be explicitly coded ,otherwise stack overflow may happen
  • A recursive function is often confusing. It is difficult to trace the logic of the function. Hence, it is difficult to debug a recursive function.

15) What are the standard predefined macros?

ANSI C has six predefined macro. They are

  • __FILE__ - Name of the current file
  • __LINE__ - Current line number
  • __TIME__ - Current time of compilation
  • __DATE__ - Current date of compilation
  • __cplusplus - If program compiled in C++ compiler
  • __STDC__ - If ANSI C is followed by the compiler strictly.
Page 2 of 8

Popular Videos

communication

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Advanced C Interview Questions and Answers for Experienced
Embedded C Interview Questions and Answers