10

What are default values for arrays like this:

char c[20];

?

BTW, Are there any?

gbn
  • 422,506
  • 82
  • 585
  • 676
oneat
  • 10,778
  • 16
  • 52
  • 70
  • Related is that reading an uninitialized variable leads to [undefined behavior](http://stackoverflow.com/questions/4259885/why-do-i-see-strange-values-when-i-print-uninitialized-variables/4259991#4259991). – GManNickG Jan 16 '11 at 20:58

4 Answers4

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
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.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 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: http://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). – conio Jan 16 '11 at 22:09
  • ok the values are defined as undeterminate - is everyone happy now? – Martin Beckett Jan 17 '11 at 00:28
2

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

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • Is there a technical difference between undefined and indeterminate? – Martin Beckett Jan 16 '11 at 21:02
  • @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. – Cheers and hth. - Alf Jan 16 '11 at 21:48
  • +1 for anonymous drive-by downvote. it's ungood that SO attracts so many people of that kind, what can be done? – Cheers and hth. - Alf Jan 16 '11 at 21:49
  • @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: http://stackoverflow.com/questions/4704567/function-with-parameter-type-that-has-a-copy-constructor-with-non-const-ref-chose/4704741#4704741 – Edward Strange Jan 16 '11 at 21:57
  • @Noah: well, now that I've upvoted, I'm considering downvoting, cause it ain't an answer to question, nor a discussion item that won't fit as comment. but i'd have to create a sock puppet for that. argh. ;-) – Cheers and hth. - Alf Jan 16 '11 at 21:59
  • 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. – Edward Strange Jan 16 '11 at 22:07
  • @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. – MSalters Jan 17 '11 at 08:30
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 :))

Vladimir
  • 1,781
  • 13
  • 12
  • "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++. – Edward Strange Jan 16 '11 at 21:56
  • 1
    @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 – Vladimir Jan 16 '11 at 22:01
  • 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. – Edward Strange Jan 16 '11 at 23:17