在C中打印ASCII代码值及其等效字符

时间:2015-03-31 10:48:42

标签: c char printf ascii

如何为给定的ASCII码值打印char?..

我看到了这个question,我的问题很相似,只是相反

以下是我的计划,但它不起作用。

int main (){
int code;     // code that user enters
char ch;      //"codes" related ASCII charcter

printf("Enter a ASCII code value: \n");
scanf("%d", &code);
ch=code;

printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);

system("PAUSE");
return 0;}  

2 个答案:

答案 0 :(得分:2)

在您的代码中,您必须更改printf语句

printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);

printf(" %c is cherechter that have %d ASCII code\n", ch ,code);

因为要打印,您不需要提供变量的地址。变量名就足够了。

答案 1 :(得分:0)

将您的代码更改为:

int main (){
char code;   //change from int to char  
char ch;      

printf("Enter a ASCII code value: \n");
scanf("%c", &code);//ASCII is a character not integer
ch=code;

printf(" %c is cherechter that have %x and %d ASCII code\n", ch ,code,code);//don't print the address access the value

system("PAUSE");
return 0;}