2017-05-25 152 views
3

我基本上想知道要放什麼東西在過去printf語句第一%d,因爲從我的理解getchar將輸入的字符的ASCII碼。那麼如何顯示輸入的字符呢?如何使用getchar將ASCII代碼轉換爲相應的int?

#include <stdio.h> 

int main(void) { 
    int c; 
    printf("Enter a character: "); 
    c = getchar(); 
    printf("The ASCII code for the character %d is %d\n", what to put here, c); 
    return 0; 
} 
+2

'的printf(「爲字符%C的ASCII碼爲%d \ n 」,C,C);' –

+0

您的標題似乎是你實際上問相反。你不想知道如何顯示整數,你想知道如何將代碼轉換回一個字符。 – Barmar

+0

字符*是*其ASCII碼(如果您的系統使用ASCII碼),所以沒有轉換。 –

回答

3

您需要提供%c格式說明格式字符串而不是%d

printf("The ASCII code for the character %c is %d\n", c, c);