Yes, this is one of the correct ways.
For c
Quoting C11, chapter §6.7.9
If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.
and, regarding the initialization for static storage variables,
If an object that has static or thread storage duration is not initialized
explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules,
and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these
rules, and any padding is initialized to zero bits;
For c++
Quoting C++17, chapter § 11.6.2
If there are fewer initializers than there are array elements, each element not explicitly initialized shall be
zero-initialized.
So, in your case,
char a[20]="\0";
try to initialize the a[0] to '\0', a[1] to '\0' (for null-terminator) and the remaining as 0. FWIW, '\0' has a decimal value of 0, so in this case, all the elements in the array are going to have a value 0.
Some similar initialization statements would be
char a[20] = "";
char a[20] = {0};
char a[20] = {'\0'};
For C++, as suggested in the other answer, including all the previous syntax,
char a[20] = {};
would also work.
char a[20]={0, 0}which works because of port70.net/~nsz/c/c11/n1570.html#6.7.9p21char a[20] = "";A string definition does not need an explicit terminator.