Printing chars and their ASCII-code in C

2019-01-07 15:18发布

问题:

How do I print a char and its equivalent ASCII value in C?

回答1:

This prints out all ASCII values:

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

and this prints out the ASCII value for a given character:

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


回答2:

Try this:

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

The %c is the format string for a single character, and %d for a digit/integer. By casting the char to an integer, you'll get the ascii value.



回答3:

To print all the ascii values from 0 to 255 using while loop.

#include<stdio.h>

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


回答4:

Chars within single quote ('XXXXXX'), when printed as decimal should output its ASCII value.

int main(){

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

    return 0;

}

Output:

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


回答5:

Nothing can be more simple than this

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

Source: program to print ASCII value of all characters



回答6:

This reads a line of text from standard input and prints out the characters in the line and their ASCII codes:

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


回答7:

void main() {

printf("%d",'a'); //We can replace a with our choice of character to get its ASCII value//

getch();

}



回答8:

#include"stdio.h"

#include"conio.h"//R.M.VIVEK coding for ascii display values

void main()
{

    int rmv;

    for(rmv=0;rmv<=256;rmv++)
        if(printf("%c",rmv))
            getch();
}


回答9:

I was using "for" This is my solution:

using System;
using System.Text;

    class PrintASCIITable
    {
        static void Main()
        {
            byte symbols = 255;

            for (int i = 0; i < symbols; i++)
            {
                Console.WriteLine((char)i);
            }
        }
    }


回答10:

#include<stdio.h>
#include<conio.h>
void main()
{
   int i;
   char ch;
   clrscr();
   printf("\t Enter a Value: ");
   scanf("%c",&ch);
   i=0;
   while(i<=ch)
   {
        printf("\t %d Is %c \n",i,i);
        i=i+1;
    }
   getch();
}


回答11:

This should work in any C system, not only those which are ASCII or UTF-8 based:

printf ( " Q is decimal 81 in ASCII\n" );

You did ask for a char; the other 96 of them are left as an exercise for the reader.



回答12:

Also you can use like this(c++):

int main(){
      int x;
      cin >> x;
      cout <<(char)x;  // In the form of character
      char a;
      cin >> a;
      cout << (int)a;  // In the form of `Ascii` code
}


标签: c printf