【问题标题】:Printing chars and their ASCII-code in C在 C 中打印字符及其 ASCII 码
【发布时间】:2009-09-24 15:52:09
【问题描述】:

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

【问题讨论】:

  • 到目前为止,12个答案中似乎没有正确答案。由于 ASCII 是 7 位编码,因此许多无法将值限制在 0 到 127 范围内,到目前为止,还没有一个解决了 C 中字符的数值不必是 ASCII 的问题i> 价值!系统/编译器也可以使用类似 EBCDIC 编码的东西,那么“a”的数值不会是 C 中“a”的 ASCII 值。

标签: c printf


【解决方案1】:

这会打印出所有 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;
}

【讨论】:

  • 您的代码打印通过将 char 转换为 int 创建的十进制值。这可能是某些系统上的 ascii 代码,但又可能不是。
  • 为什么上面的程序不打印127到160的ASCII字符?
  • 相当肯定 ASCII 在 127 处停止
  • 这不是未定义的行为吗?使用%d 打印char??
  • 抱歉,getch() 为 -1。使用这个非标准函数的可怕例子。
【解决方案2】:

试试这个:

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

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

【讨论】:

  • 您不需要转换为int,因为标准类型的提升会自动将c 提升为int
【解决方案3】:

没有比这更简单的了

#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

【讨论】:

    【解决方案4】:

    使用 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;
    }
    

    【讨论】:

    • ASCII 有 7 位,所以 0 到 127 之后是系统定义的 EASCII
    【解决方案5】:
    #include<stdio.h>
     void main()
    {
    char a;
    scanf("%c",&a);
    printf("%d",a);
    }
    

    【讨论】:

      【解决方案6】:

      单引号内的字符 ('XXXXXX'),当打印为十进制时应输出其 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
      

      【讨论】:

        【解决方案7】:

        这会从标准输入中读取一行文本并打印出该行中的字符及其 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);
            }
        }
        

        【讨论】:

        • %X 需要一个 unsigned 整数。
        • @NisseEngström - 代码更正,适合真正的迂腐者。是的,将unsigned char[] 用于文本缓冲区总是一个好主意。
        【解决方案8】:

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

        这是一个例子:

        #include<stdio.h>
        int main()
        {
            //we are printing the ASCII value of 'a'
            char a ='a'
            printf("%d",a)
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-06-04
          • 1970-01-01
          • 2012-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-25
          • 2016-06-21
          • 1970-01-01
          相关资源
          最近更新 更多