I am writing a lot of C code for embedded systems lately and I always wonder if I should use signed or unsigned values for my return type. Is there a "right" way to do so?
I personally prefer the signed types, because they give me a broader range for expressing myself. But I work with a lot of legacy code which uses solely unsigned types.
Example 1 (my code):
int16_t my_initializer(void)
{
return -1; //-> error (critical)
...
return 0; //-> everything is OK
...
return 1; //-> warning (non critical)
...
return 2; //-> another warning
}
Example 2 (legacy code):
uint16_t my_initializer(void)
{
return 0; //-> everything is OK
...
return 1; //-> warning (non critical)
...
return 2; //-> another warning or an error
...
}
I took a look in the linux kernel coding style and the Google C++ style guide but I found no clue whether to use the signed or unsigned types. Is there a "best practice" out there?