27

If I have the following:

char test[10] = "#";

Is test[1] through test[9] guaranteed to be \0? Or is only test[1] guaranteed to be \0?

1 Answer 1

39

This definition

char test[10] = "#";

is equivalent to

char test[10] = { '#', '\0' };

That is two elements of the array are initialized explicitly by the initializers. All other elements of the array will be zero initialized that is implicitly they will be set to '\0'

According to the C++ Standard (section 8.5.2 Character arrays)

3 If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized (8.5).

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

1 Comment

Thanks, do you have the line from the specification that states this?

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.