HOWTO · C
How to Initialize an Array to 0 in C
Learn how to initialize a C array to zero with {0}, a loop, or memset, and when C zero-initializes an array automatically.
On this page
To initialize every element of an int array to zero when you declare it, use int values[5] = {0};. C initializes the first element from 0 and initializes every remaining element of the aggregate to zero as well. If the array already exists, assign each element in a loop. memset can fill an array’s bytes with zero, but byte filling is not a general substitute for typed initialization.
Initialize an array to zero at declaration
Use an initializer when you know the array’s size at its declaration. The following complete example prints the five initialized values.
#include <stdio.h>
int main(void) {
int values[5] = {0};
for (size_t i = 0; i < sizeof values / sizeof values[0]; ++i) {
printf("%d%s", values[i], i == 4 ? "\n" : " ");
}
return 0;
}
0 0 0 0 0
The {0} initializer explicitly gives the first element zero; aggregate initialization supplies zero for the elements you omit. You can also omit an initializer for an object with static or thread storage duration: C zero-initializes it automatically. An ordinary uninitialized local array has automatic storage duration, so its elements are not automatically set to zero. Add an initializer or assign the elements before reading them.
Initialize an array to zero with a loop
A loop is useful when the array has already been declared or when the values are chosen during execution. This complete example uses the array’s element count as the loop bound and prints the result.
#include <stdio.h>
int main(void) {
int values[5];
size_t count = sizeof values / sizeof values[0];
for (size_t i = 0; i < count; ++i) {
values[i] = 0;
}
for (size_t i = 0; i < count; ++i) {
printf("%d%s", values[i], i + 1 == count ? "\n" : " ");
}
return 0;
}
0 0 0 0 0
The sizeof expression works here because values is an array in the same scope, not a pointer parameter. If you pass an array to a function, pass its length separately; inside that function the parameter is adjusted to a pointer and sizeof gives the pointer size.
Use memset() carefully
The standard library function memset writes the same byte value across a memory region. Include <string.h> and use sizeof values to cover the entire in-scope array, as in this complete example.
#include <stdio.h>
#include <string.h>
int main(void) {
int values[5];
memset(values, 0, sizeof values);
for (size_t i = 0; i < sizeof values / sizeof values[0]; ++i) {
printf("%d%s", values[i], i == 4 ? "\n" : " ");
}
return 0;
}
0 0 0 0 0
The third argument is a byte count, so sizeof values is correct for this array. memset does not assign a typed value to each element; it writes an all-bits-zero representation. Use it only when that representation is appropriate for the element type and platform. For portable semantic zero-initialization of an array at declaration, prefer {0}. Do not assume that zero bytes represent a null pointer or floating-point zero on every C implementation.
Initialize an array to values other than zero
List the values you want in the initializer, for example int values[] = {2, 4, 6};. C infers the outer dimension from the number of initializers. For a multidimensional array, only the outer dimension may be omitted in this form: int grid[][3] = {{1, 2, 3}, {4, 5, 6}};; the inner dimension must be specified.
GNU range designators such as int values[5] = {[0 ... 4] = 7}; are a GCC extension, not portable standard C syntax. Use an ordinary initializer list or a loop when portability across C compilers matters. A compound literal such as (int[]){1, 2, 3} creates an unnamed array with block lifetime in this function context; it is not dynamic allocation and does not replace a named array declaration for general initialization.