Which C library function is used to allocate memory for a specified number of elements and initialize the allocated memory to zero?
Options
- A. calloc()
- B. realloc()
- C. memcpy()
- D. More than one of the above
- E. None of the above
Quiz Practice:
A. calloc()
Answer: (A) calloc()
calloc() is a C library function used for dynamic memory allocation.
Unlike malloc(), which allocates a block of memory without initializing its contents, calloc() initializes the allocated memory to zero.
Its general form is calloc(number_of_elements, size_of_each_element).
For example, calloc(5, sizeof(int)) requests memory for five integers and initializes the allocated bytes to zero.