在C中打印字符及其ASCII码

时间:2009-09-24 15:52:09

标签: c printf

如何在C中打印char及其等效的ASCII值?

8 个答案:

答案 0 :(得分:32)

这将打印出所有ASCII值:

int main()
{
    int i;
    i=0;
    do
    {
        printf("%d %c \n",i,i);
        i++;
    }
    while(i<=255);
    return 0;
}

并打印出给定字符的ASCII值:

int main()
{
    int e;
    char ch;
    clrscr();
    printf("\n Enter a character : ");
    scanf("%c",&ch);
    e=ch;
    printf("\n The ASCII value of the character is : %d",e);
    getch();
    return 0;
}

答案 1 :(得分:4)

试试这个:

char c = 'a'; // or whatever your character is
printf("%c %d", c, c);

%c是单个字符的格式字符串,%d是数字/整数。通过将char转换为整数,您将获得ascii值。

答案 2 :(得分:1)

使用while循环打印从0到255的所有ascii值。

#include<stdio.h>

int main(void)
{
    int a;
    a = 0;
    while (a <= 255)
    {
        printf("%d = %c\n", a, a);
        a++;
    }
    return 0;
}

答案 3 :(得分:1)

#include<stdio.h>
 void main()
{
char a;
scanf("%c",&a);
printf("%d",a);
}

答案 4 :(得分:0)

这将从标准输入中读取一行文本,并打印出行中的字符及其ASCII代码:

#include <stdio.h>

void printChars(void)
{
    unsigned char   line[80+1];
    int             i;

    // Read a text line
    if (fgets(line, 80, stdin) == NULL)
        return;

    // Print the line chars
    for (i = 0;  line[i] != '\n';  i++)
    {
        int     ch;

        ch = line[i];
        printf("'%c' %3d 0x%02X\n", ch, ch, (unsigned)ch);
    }
}

答案 5 :(得分:0)

单引号(&#39; XXXXXX&#39;)中的字符,以十进制格式打印时应输出其ASCII值。

int main(){

    printf("D\n");
    printf("The ASCII of D is %d\n",'D');

    return 0;

}

输出:

% ./a.out
>> D
>> The ASCII of D is 68

答案 6 :(得分:0)

没有什么比这更简单了

#include <stdio.h>  

int main()  
{  
    int i;  

    for( i=0 ; i<=255 ; i++ ) /*ASCII values ranges from 0-255*/  
    {  
        printf("ASCII value of character %c = %d\n", i, i);  
    }  

    return 0;  
}   

来源:program to print ASCII value of all characters

答案 7 :(得分:0)

打印给定字母的ASCII值的最简单方法。

这里是一个例子:

# Add ci dep
yarn add codecov --frozen-lockfile

# Install all deps from yarn.lock
yarn install --frozen-lockfile
相关问题