Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to define an array of 0 in C language
How to define an array of 0 in C language

If you want to define a 0-length array, it cannot be implemented in C language, because C language does not support arrays with a length of 0. In fact, this is meaningless.

To set the initial value of the defined array to 0, different operations can be performed based on the type of the array:

1 Local variables. Local variables can be initialized when they are defined.

type name[N] = {0};

From a performance point of view, the first element is assigned a value of 0, but the C language convention requires explicit partial initialization. In the case where there is no explicit initialization, the value of the part is automatically assigned to 0, so the above writing method can initialize the entire array to 0.

2 Global variables or static local variables.

For these two variables, you can also use the method 1, but a more concise way of writing is not to initialize, such as

type name[N];

Or

static type name[N];

Because the C language stipulates that when global variables or static local variables are not explicitly initialized, the default initialization value is 0. Therefore, not initializing has the same effect as initializing to 0, and the code is simpler and the execution is more efficient.