奇怪的unsigned char cast

sho*_*nex 6 c

使用的目的/优势/区别是什么?

/* C89 compliant way to cast 'char' to 'unsigned char'. */
static inline unsigned char
to_uchar (char ch)
{
  return ch;
}
Run Code Online (Sandbox Code Playgroud)

与标准演员相比?

编辑:在gnulib中的base64代码中找到

pmg*_*pmg 1

也许编写该函数的程序员不喜欢强制转换语法......

foo(to_uchar(ch));      /* function call */
foo((unsigned char)ch); /* cast */
Run Code Online (Sandbox Code Playgroud)

但无论如何我都会让编译器担心它:)

void foo(unsigned char);
char s[] = "bar";
foo(s[2]); /* compiler implicitly casts the `s[2]` char to unsigned char */
Run Code Online (Sandbox Code Playgroud)