ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int *createZeroed(int n) {
int *arr = calloc(n, sizeof(int));
return arr;
}
int main(void) {
int *arr = createZeroed(5);
printf("%d %d %d", arr[0], arr[2], arr[4]);
free(arr);
}- A. 0 0 0
- B. Undefined behaviour: calloc does not guarantee zero-initialisation
- C. Garbage garbage garbage
- D. 1 1 1
Correct answer: A. 0 0 0
Unlike malloc, calloc is specifically guaranteed by the C standard to zero-initialise the entire allocated block. Every element of the returned array is reliably 0 until explicitly written, so arr[0], arr[2], arr[4] all print 0.
More C Programming questions
- Assume 32-bit int. What is printed? c int x = 5; int y = x-- - --x; printf("%d %d", x, y);
- Assume a 64-bit LP64 ABI. What is printed? c struct Pair { int first; int second; }; struct Pair makePair(int a, int b) { return (struct…
- What is printed? c int main(void) { int a = 10, b = 3; printf("%d %d %f", a / b, a % b, (float)a / b); }
- What is printed? c int main(void) { char ch = 'A'; int code = ch + 1; printf("%d %c", code, (char)code); }
- What is printed? c int main(void) { int n = 17; int isPrime = 1; for (int i = 2; i i <= n; i++) { if (n % i == 0) { isPrime = 0; break; }…
- What is printed? c int main(void) { int a = 5; int b = a; b = b + 10; int p = &a; p = p + 100; printf("%d %d", a, b); }