如何使用getchar将ASCII代码转换为相应的int?

时间:2017-05-25 09:18:32

标签: c int ascii string-formatting getchar

我基本上想知道在第一个printf的最后%d语句中放入什么,因为根据我的理解getchar将输入的字符转换为ASCII码。那么如何显示输入的字符呢?

#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;
}

1 个答案:

答案 0 :(得分:3)

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

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