簡體   English   中英

在位操作中使用“ char”變量

[英]Using 'char' variables in bit operations

我使用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可以是有signed也可以是unsigned因此,如果您需要unsigned char ,則應明確指定它,這對於那些閱讀您的代碼的人來說很清楚,而不是依賴於編譯器設置。

c99標准草案的相關部分來自6.2.5 類型第15段

char,signed char和unsigned char這三種類型統稱為字符類型。 實現應將char定義為與簽名char或未簽名char具有相同的范圍,表示形式和行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM