1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In C, what does the expression sizeof(a) / sizeof(a[0])…

Added 30 Nov -0001

In C, what does the expression sizeof(a) / sizeof(a[0]) evaluate to when a is declared as int a[5]; on a machine where int is 4 bytes?

  1. 1, because the two operands cancel out
  2. 5, the number of elements in the array Correct answer
  3. 20, the total size of the array in bytes
  4. 4, the size of one integer element

Correct answer

B. 5, the number of elements in the array

14 views

MCQSEXAM

Explanation

The sizeof operator reports the storage size of its operand in bytes, so sizeof(a) is 20 and sizeof(a[0]) is 4, and dividing them gives the element count 5. This is the standard C idiom for finding array length without a separate variable. It works only while a is still an array: once the array decays to a pointer in a function parameter, sizeof(a) becomes the pointer size, so the idiom silently breaks there.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? 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