1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Consider void f(int a[10]) { printf("%zu", sizeof a); }…

Added 30 Nov -0001

Consider void f(int a[10]) { printf("%zu", sizeof a); } called with an int arr[10] on a 64-bit LP64 platform. What does the sizeof expression print?

  1. 8, because the parameter is adjusted to int * and sizeof then measures a pointer Correct answer
  2. 40, because the declared bound of 10 ints is part of the parameter type
  3. 4, because the array name always decays to a single int
  4. 80, because the decayed pointer is doubled for bounds checking

Correct answer

A. 8, because the parameter is adjusted to int * and sizeof then measures a pointer

13 views

MCQSEXAM

Explanation

Array-to-pointer decay means the declaration int a[10] in a parameter list is adjusted to int *, so sizeof inside f measures a pointer, which is 8 bytes under LP64. The original array in its defining scope would give 40, since sizeof applied to an array type yields the whole array size. The classic exam trap is using the sizeof(a)/sizeof(a[0]) idiom inside a callee, where it silently gives 8/4 = 2 instead of 10.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs