10

What are default values for arrays like this:

char c[20];

?

BTW, Are there any?

1
  • Related is that reading an uninitialized variable leads to undefined behavior. Commented Jan 16, 2011 at 20:58

4 Answers 4

18

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

5 Comments

Is there a technical difference between undefined and indeterminate?
@Martin: there is. "indeterminate value" is a term employed by the C++ standard. you can search for it. and more importantly, good C++ programmers know what you mean when you use that term. "undefined value" is not such a term: not standard, not well-known.
@Alf - thanks, but you can't get worried about votes here on SO and quite specifically in the C++ tag. Politics have more sway here than correctness. Just have a look here: stackoverflow.com/questions/4704567/…
What you mean it's not an answer to the question? The standard specifically states that the values are indeterminate and that's what I've just stated here in the answer. Bailey's answer is MORE correct (and I upvoted accordingly) but that doesn't mean this isn't an answer.
@Martin Beckett: Yes. You can memcpy() indeterminate values. "Undefined values" as in "values after undefined behavior" are so meaningless that you can't memcpy() them.
2

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.

2 Comments

As said below, this is NOT undefined. The difference between undefined behavior and other categories of unknown was discussed in several SO questions (top results on google when I looked for the link coming next), and in my opinion the best explanation of what that means in on c-faq: c-faq.com/ansi/undef.html (the fact we're talking about C++ rather than C doesn't really matter here, and every programmer should read this enter FAQ at least twice).
ok the values are defined as undeterminate - is everyone happy now?
1

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 :))

3 Comments

"undefined" vs. "indeterminate" are C++ terms. They may or may not be mathematical terms but any C++ developer should be VERY aware of them and the difference as they apply to programming in C++.
@Noah - wow, you're very smart programmer, it seems :) But, looking for differences between terms, you could leave behind programming.. Anyway, I think that is important to understand each other; I accepted your suggestion, I wrote it and there is no necessity to argue about it. Regards
That's fine. I'd have reverted my -1 though if you weren't implying that it wasn't important to a programmer to know the difference, something I think is seriously mistaken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.