Default values in array

前端 未结 4 1490
逝去的感伤
逝去的感伤 2021-01-02 05:39

What are default values for arrays like this:

char c[20];

?

BTW, Are there any?

相关标签:
4条回答
  • 2021-01-02 06:22

    If declared at namespace scope then c will have static storage scope and will be zero-initialized so every element of c will have value '\0'.

    If declared in a function then c will not be initialized. The initial value of the elements of c will be indeterminate.

    0 讨论(0)
  • 2021-01-02 06:29

    As it is already said - values are indeterminate. But, I have to mention that if your array is static or global, the values are initialized to their default values, which usually means they will be initialized to zeros (or '\0' in the case of array of chars).

    EDIT: As Noah Roberts suggested, the term "indeterminate" is probably more appropriate than "undefined" (strictly mathematically speaking) - so suggestion is accepted and I've changed the term to "indeterminate". But majority of us are engineers or programmers here, not mathematicians (I suppose) and similar omissions should be forgiven :))

    0 讨论(0)
  • 2021-01-02 06:34

    Undefined or in less technical language - random.

    Your compiler (at least in debug mode) may set it to a particular value for testing.

    And of course from a strict Copenhagen interpretation of quantum theory the value it does have is both indeterminate (used in it's correct sense) until you collapse the wavefunction by measuring it.

    0 讨论(0)
  • 2021-01-02 06:42

    This in NOT 'undefined'. It is 'indeterminate'. The values are simply are not known.

    0 讨论(0)
提交回复
热议问题