ccastingmagic-numberssplint

C Character from literal Splint warns of incompatible types


I have a program that uses unsigned chars to represent integers with a small range. I find myself needing to clear them to 0 in several different parts of the program, I have also recently started using splint and apparently:

unsigned char c = 0;

gives the warning:

Variable c initialized to type int, expects unsigned char: 0
  Types are incompatible.

As there is no suffix for a literal char, How is it best to resolve this? I think I have a few options:

1: Ignore the warning.

2: Cast every time:

unsigned char c = (unsigned char)0;

3: Make a var to cut down the length of the code:

unsigned char uc_0 = (unsigned char)0; 
unsigned char c = uc_0;

4: A function:

static inline unsigned char uchar(int in)
{
   return (unsigned char)in;
}
unsigned char c = uchar(0);

Solution

  • splint has an option +charint that will treat char int as interchangeable.