ProgrammingHard
int *a = calloc(4, sizeof *a); int sum = 0; for (int i = 0; i < 4; i++) sum += a[i]; printf("%d", sum); What is printed?
- A. 4
- B. -1
- C. garbage
- D. 0
Correct answer: D. 0
calloc sets every element to zero, so the sum of the four ints is 0. malloc alone would leave the values indeterminate.
More C Programming questions
- Which statement about calling free on a null pointer is correct in C?
- A pointer p is freed with free(p) and then set to NULL. Why is this safer than leaving it dangling?
- Which code creates a flexible structure holding n ints after a header, with a single allocation and correct size?
- A tail-insertion function takes a pointer to the head pointer, so it can work on an empty list. Which loop finds the link to change?
- A queue with head and tail pointers is emptied by dequeuing its only node. What must the dequeue do to keep the queue correct?
- A recursive function removes all nodes equal to x from a singly linked list and returns the new head. Which return statement is the right…