开发者

Default values in array

开发者 https://www.devze.com 2023-02-05 11:55 出处:网络
What are default values for arrays like this: char c[20];开发者_StackOverflow ? BTW, Are there any?If declared at namespace scope then c will have static storage scope and will be zero-initialized

What are default values for arrays like this:

char c[20];开发者_StackOverflow

?

BTW, Are there any?


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.


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消