Initializing char Array with Smaller String Literal

后端 未结 4 2014
甜味超标
甜味超标 2021-01-04 09:29

If I write:

char arr[8] = \"abc\";

Is there any specification over what arr[4] might be? I did some tests with Clang and it se

4条回答
  •  执笔经年
    2021-01-04 09:51

    This is standard behavior. Every element of an array that is not explicitly initialized is initialized to a default value ('\0' for char), if any prefix of the array is initialized in the declaration. It also works for other types:

    int a[10] = {1};
    

    zeroes out a[1] through a[9].

提交回复
热议问题