如何打印出两个字符的ASCII值?

4

我想打印"\t"即转义序列的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;

}

'\t' 是一个字符。它是一个制表符。当你在字符串字面值中写它时,你使用两个字符来表示它。但是当你输入时,它会将其作为一个反斜杠和一个 t 的字面值。例如:'\t'。 - Oliver Ni
2
如果你在键盘上按 TAB 键而不是输入 \t,你会发现得到了预期的结果,即 9。这是因为 \t 只是表示水平制表符的一种方式。(这可能是一个不错的阅读材料,帮助理解发生了什么。) - Matt Gibson
那么如何打印出"\t"这两个字符的ASCII值呢?我必须显示"\t"的ASCII值,整个过程是否可能? - Harsh patel
我不确定你的问题完全清楚。如果您在程序中输入反斜杠后跟一个t,您期望输出是什么?能否给我们举个例子? - Matt Gibson
这就是我想要找到的,如果我通过上述程序输入\t或\n或\a等字符来查找它的值会发生什么。 - Harsh patel
3个回答

2
捕获反斜杠的输入并将其处理为单独的输入。可以使用开关打印转义字符的结果。
#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

要获取反斜杠的代码,必须输入两个反斜杠


非常感谢您,xing。这正是我想要的。但如果您能帮忙解释一下 "1 == scanf(" %c",&b)" 和 "( b == '\')" 就更好了。 - Harsh patel
但是,当我写入数字2而不是1时,第二个printf语句没有被打印出来,当我写3的时候也是同样的情况。能否请您解释一下这些? - Harsh patel
当我写下 "if ( 2 == scanf(" %c",&b))" 这个语句时,输出中不会打印出 "printf("The ASCII value of '%c' is %d\n",b,b);" 这个语句。这就是我的意思。 - Harsh patel
好的,现在我明白了问题。非常感谢 Xing 提供的帮助。 - Harsh patel
printf("The ASCII value of '\\t' is %d\n", '\t'); only prints the ASCII value of '\t' if the source encoding is ASCII (very common). To print the ASCII value of '\t', use printf("The ASCII value of '\\t' is %d\n", 9); - chux - Reinstate Monica
@xing "需要查找表" --> 可能。 - chux - Reinstate Monica

2
这段代码可能有助于您理解这里发生了什么:
#include <stdio.h>

const char* escaped(int ch) {
    switch (ch) {
    case '\t': return "tab";
    case '\n': return "newline";
    case '\r': return "carriage return";
        // continue with other escaped characters
    default: return "not escaped";
    }
}

int 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 and is also known as: %s\n", b, b, escaped(b));
}

为了更加清晰,对于一个制表符,在键盘上只需按下tab键即可。不需要输入字符串"\t"。字符串"\t"将被解释为两个字符:'\'和't'。

转义字符\是您在编写C代码中使用的内容。例如,如果您在某个C源代码中键入字符串"trying",则输入的字符流为:t r y i n g,但如果您键入字符串:"\trying",那么第一个字符表示这是一个转义字符,并且是一种方便的方式来指示您真正想要一个制表符,后跟字符:r y i n g。

通过上述代码,如果您输入"\t",scanf仅从stdin获取一个字符,因此它只取第一个字符,也就是反斜杠(转义序列的开头),并将打印出:

Enter any character to print it's ASCII value : \t
The ASCII value of '\' is 92 and is also known as: not escaped

't'字符仍然在输入流中,程序尚未处理此字符。
scanf不将"\t"解释为制表符,而是将其解释为字符流\后跟t。这会导致您的困惑。
您将在运行此代码时遇到一些问题。例如,您想尝试退格,即\b或十进制8值。 scanf可能会忽略它,并假定您正在尝试删除先前的字符。但对于其他转义字符,如制表符和换行符,它将起作用。
阅读此文:https://en.wikipedia.org/wiki/Escape_character

0
 #define INV       (EOF -1)

int z = 0, tmp = INV;
char buff[6] = "\\0";
while (1)
{
    if (tmp == INV)
        z = getchar();
    else
    {
        z = tmp;
        tmp = INV;
    }
    if (z == EOF || tmp == EOF) break;
    if (z == '\\')
    {
        tmp = getchar();
        switch (tmp)
        {
        case 't':
            z = '\t';
            tmp = INV;
            break;
        case 'b':
            z = '\b';
            tmp = INV;
            break;
        case 'r':
            z = '\r';
            tmp = INV;
            break;
        case 'n':
            z = '\n';
            tmp = INV;
            break;

            // add here anothere ones
        default:
            break;
        }
    }
    printf("Char \\0%03o - '%c'\t\t has the ascii code of %03d decimal 0x%02X hexadecimal\n",  z, z < 32 ? ' ' : z, z, z);
}

非常感谢PeterJ的回答。但是我刚学C编程,所以对这个程序理解起来有困难。 - Harsh patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接