1. Home
  2. Computer Science MCQs
  3. C Programming
  4. int a[6] = {[4] = 7}; is compiled by a C99 compiler. What…

Added 30 Nov -0001

int a[6] = {[4] = 7}; is compiled by a C99 compiler. What are the values of a[0] and a[4]?

  1. a[0] is indeterminate and a[4] is 7, since only the named element is touched
  2. a[0] is 7 and a[4] is 0, because the first initialiser fills the leading element
  3. Both are 7, because one designated initialiser applies to the whole aggregate
  4. a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static Correct answer

Correct answer

D. a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static

15 views

MCQSEXAM

Explanation

Designated initialisers set the named element and leave the rest zero, because a partially initialised aggregate is zero-filled first. Writing int a[6] = {[4] = 7} therefore yields {0,0,0,0,7,0}. C++20 accepts the same syntax but requires designators to appear in declaration order, unlike C where any order is allowed and later designators overwrite earlier ones.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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