c - 在位操作中使用 'char' 变量

标签 c xlib

我使用 XLookupString 将键事件映射到 ASCII 字符串、keysym 和 ComposeStatus。

int XLookupString(event_structure, buffer_return,  bytes_buffer, keysym_return, status_in_out)

XKeyEvent *event_structure;
char *buffer_return; /* Returns the resulting string (not NULL-terminated). Returned value of the function is the length of the string. */
int bytes_buffer;
KeySym *keysym_return;
XComposeStatus *status_in_out;

这是我的代码:

char mykey_string;
int arg = 0;

------------------------------------------------------------

    case KeyPress:
        XLookupString( &event.xkey, &mykey_string, 1, 0, 0 );
        arg |= mykey_string;

但是在位运算中使用“char”变量,符号扩展会产生意想不到的结果。 我有可能阻止这种情况吗?

谢谢

最佳答案

char 可以是 signedunsigned 所以如果你需要 unsigned char 你应该明确指定它,它让那些阅读您的代码的人清楚地知道您的意图,而不是依赖编译器设置。

c99 标准草案的相关部分来自 6.2.5 Types paragraph 15:

The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char

关于c - 在位操作中使用 'char' 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427750/

相关文章:

使用 Xlib 关闭应用程序窗口

c - 结构中的结构

c - 在 I/O 流 -C 中找到重复出现的字符串?

c# - C#、C++ 和 C 的混合

linux - 多个 X 服务器上的 x11/xlib XCreateSimpleWindow

events - Xlib 鼠标事件和 ButtonPressMask

c++ - 许多 XSetInputFocus 和 XSync 导致错误

c - 访问长双位表示

c - 围栏密码算法c

linux - 你将如何在 Linux 上构建 "pixel perfect"GUI?