Machine-translated by Google. Questions are authored in English.
Top

Discussion

The value of an automatic variable that is declared but not initialized will be

  • A.-1
  • B.unpredictable
  • C.none of these

Answer: B

Correct answer: unpredictable

An automatic variable that is declared without an initialiser holds whatever happened to be in its stack storage, so its value is unpredictable - indeterminate, in the standard's term.

Reading it before assignment is undefined behaviour rather than merely a matter of luck: the compiler may assume it does not happen and optimise on that basis, so the observed effect need not even be a consistent garbage value. Static and external variables are the contrast, being zero-initialised before the program begins.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.