Bulk initialize the char array by "\0" instead of memset()

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.


It works almost, in a curious way, by accident and is, in my opinion, an exercise in obfuscation. An explanation:

In C++ "\0" is a const char[2] literal with value '\0' (Octal constant with value 0) followed by a NUL-terminator, i.e. both 0 values. In C it's a char[2] constant with the same value.

Initialising a to that also causes the other elements of a to be initialised to 0 too (by the C and C++ standards, the other elements are initialised as per static storage duration).

In C++ writing char a[20] = {} is sufficient, in C you need at least char a[20] = {0}.

Tags:

C++

C

Arrays

Char