1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A compound literal (int[]){1, 2, 3} appears inside a…

Added 30 Nov -0001

A compound literal (int[]){1, 2, 3} appears inside a function body in C99. What is its storage duration?

  1. Register, because the literal is a constant expression folded at compile time
  2. Automatic, matching the enclosing block, so the array dies when that block exits Correct answer
  3. Static, so the array outlives the enclosing block and can safely be returned to its caller
  4. Dynamic, because the compiler reserves the array with an implicit malloc call at run time

Correct answer

B. Automatic, matching the enclosing block, so the array dies when that block exits

15 views

MCQSEXAM

Explanation

Compound literals inside a block have automatic storage duration tied to that block, so returning (int[]){1,2,3} hands back a dangling pointer. At file scope the same syntax creates an object with static storage duration, which is why initialising a global pointer from a compound literal works. The trap is subtle because the literal looks like a constant expression.

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