Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding statements in C

I am going through some C codes. Some of them are a little difficult to understand. For instance, what does following assignment do:

MY_TYPE my_var[3]={0};

MY_TYPE is some fixed point arithmetic type. I have not yet come across variables with [] brackets and assignment with {} around values.

That was too easy, I guess. So, what's advantage of defining

my_type my_var[3]={0};

over this:

my_type my_var[3];
like image 868
user2178841 Avatar asked Dec 09 '22 14:12

user2178841


1 Answers

It creates an array my_var of type MY_TYPE that is of size 3 and is initialised to all 0s (I suspect MY_TYPE is some sort of integer type). Note that only one initialisation is necessary for the rest to be init`ed too.

Also note that if you declare the array globally as opposed to within a block, then it will be initialised automatically and this MY_TYPE my_var[3]; will be enough.

like image 144
Nobilis Avatar answered Dec 11 '22 10:12

Nobilis