2013-08-25 40 views
1

我使用XLookupString将一个关键事件映射到ASCII字符串,keysym和ComposeStatus。在位操作中使用'char'变量

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; 

但使用位操作“字符”变量,符号扩展会产生意想不到的效果。 我可以防止这个?

感谢

+3

从本质上讲,位运算是基于无符号整数。所以建议你应该使用unsigned char和unsigned int来代替。顺便说一句,VC++默认将char视为未签名,您可以通过编译器选项指定char类型的符号。 – xmllmx

回答

0

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

C99标准草案的相关部分是从6.2.5类型第15段

三种类型Char,符号的字符,和unsigned char统称 字符类型。实现应定义字符具有相同的范围, 表示,和行为,无论是签署char或unsigned char