How to print 2-character ASCII value?

I'm trying to print the ASCII value of the sequence "\ t" ie But my program only prints the ASCII value "\" ie the ASCII value will be 92. Is there a way to print the ASCII value of 2 characters? Help would be really appreciated. I have included my code below.

#include<stdio.h>

main()

{

    char b=0;

    printf("Enter any character to print it ASCII value : ");

scanf("%c",&b);

printf("The ASCII value of '%c' is %d",b,b);

return 0;

}
+4
source share
3 answers

Capturing a backslash input and treating it as a separate input. The switch can be used to print the result for escaped characters.

#include <stdio.h>

int main( void) {
    char b=0;

    printf("Enter any character to print it 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;
}

Output

Enter any character to print it. ASCII value: \ t
The ASCII value '\ t' is 9

. ​​ ASCII:\\
ASCII '\' 92

,

+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 ASCII value : ");
    scanf("%c", &b);
    printf("The ASCII value of '%c' is %d and is also known as: %s\n", b, b, escaped(b));
}

, , , . "\ t". "\ t" 2 : "\" "t".

escape- - , C. , "try" C, : , : "\ try", , , , : ryin g.

, "\ t", scanf stdin, , , ( escape-)

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

't' , .

scanf "\ t" , \, t. .

. , backspace, \b 8. scanf, , , . , , .

: https://en.wikipedia.org/wiki/Escape_character

+2
 #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);
}
0
source

Source: https://habr.com/ru/post/1683578/


All Articles