Don't do that, in general this is just useless and error prone. Variables with static allocation are initialized to 0
from the start. There is no need to initialize them. In addition the compiler chooses the correct 0
to do so, e.g 0.0
for doubles or a null pointer constant for pointers etc.
If you want to make it clear for you and the readers of the code you could give it an explicit initializer, e.g
static int var_1 = { 0 };
This fall back initializer should always work. Even better, if you have a C99 compatible compiler, there are designated initializers:
static int arr[3] = { [0] = 0, [1] = 42, };
The fields that are not mentioned in an initializer are again by default initialized to 0
, here e.g the element arr[2]
.
All of this explicit initialization works for statically allocated variables or for stack variables (auto
).
memset
is used way to often for that purpose. Just let the compiler do it for you. In the worst case he will just do a memset
, but often he has a more clever way to do it.