我正在尝试打印“\t”的 ASCII 值,即转义序列。 但我的程序只打印“\”的 ASCII 值,即 ASCII 值将是 92。 有没有办法打印 2 个字符的 ASCII 值? 非常感谢您的帮助。我在下面包含了我的代码。
#include<stdio.h>
main()
{
char b=0;
printf("Enter any character to print it's ASCII value : ");
scanf("%c",&b);
printf("The ASCII value of '%c' is %d",b,b);
return 0;
}
最佳答案
捕获反斜杠的输入并将其作为单独的输入处理。可以使用一个开关来打印转义字符的结果。
#include <stdio.h>
int main( void) {
char b=0;
printf("Enter any character to print it's ASCII value : ");
if ( 1 == scanf(" %c",&b)) {
if ( b == '\\') {//read a backslash
if ( 1 == scanf(" %c",&b)) {
switch ( b) {
case 't' :
printf("The ASCII value of '\\t' is %d\n", '\t');
break;
case '\\' :
printf("The ASCII value of '\\' is %d\n", '\\');
break;
default :
printf ( "not handled yet\n");
}
}
}
else {
printf("The ASCII value of '%c' is %d\n",b,b);
}
}
return 0;
}
输出
输入任何字符以打印其 ASCII 值:\t
'\t'的ASCII值为9
输入任何字符以打印其 ASCII 值:\\
'\'的ASCII值为92
要获取反斜杠的代码,必须输入两个反斜杠
关于c - 如何打印 2 个字符的 ASCII 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650530/