如何打印 2 个字符的 ASCII 值?

How to print ASCII value of 2 characters?

我正在尝试打印“\t”的 ASCII 值,即转义序列。 但我的程序只打印“\”的 ASCII 值,即 ASCII 值将是 92。 有没有办法打印 2 个字符的 ASCII 值? 帮助真的 appreciated.I 在下面包含了我的代码。

#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

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

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

这段代码可能会帮助您理解这里发生的事情:

#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" 将被解释为 2 个字符:'\' 和 't'.

\ 转义码是您在 C 代码中编写字符串时会用到的东西。 例如,如果您在某些 C 源代码中键入字符串 "trying",那么您将输入字符流:t r y i n g 但如果您键入字符串:“\trying”,则第一个字符表示这是一个转义字符,是一种方便的方式来表明您真的想要一个制表符后跟字符:r y i n g.

使用上面的代码,如果您输入“\t”,scanf 一次只从标准输入获取一个字符,所以它只获取第一个字符,即反斜杠(转义序列的开始),并将打印出来:

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