34

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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Chris_45
  • 8,769
  • 16
  • 62
  • 73
  • 1
    So far there seems to be no correct answer among the 12 answers. Many fail at limiting the values to the 0 to 127 range as ASCII is a 7 bit encoding, and so far none has solved the problem that the numerical value of a character in C doesn't have to be the *ASCII* value! The system/compiler could also be using something like EBCDIC encoding, then the numerical value of an 'a' would not be the ASCII value of an 'a' in C. – BlackJack Aug 17 '17 at 17:01

8 Answers8

43

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;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • 5
    Your code prints the decimal value created by converting the char to an int. This may be the ascii code on some systems, but then again it may not. – Pete Kirkham Sep 24 '09 at 16:24
  • 1
    Why the above program doesnt print ASCII characters from 127 to 160? – CodeCodeCode May 03 '13 at 12:30
  • 6
    Pretty sure ASCII stops at 127 –  Sep 23 '13 at 23:49
  • 4
    Isn't this undefined behavior?? using `%d` to print a `char`?? – user007 Jul 10 '15 at 16:10
  • 2
    Sorry, -1 for `getch()`. Terrible example using this non standard funcion. – Iharob Al Asimi Jan 18 '16 at 20:32
  • 1
    @user007: No. It is not possible to pass a `char` to a variadic function. Any integer type that is narrower than `int` will be converted to `int` or `unsigned int`. The only case that can cause trouble would be an unusual implementation where `CHAR_MAX` is not representative in a `signed int`, but would cause all sorts of trouble elsewhere. – Nisse Engström Aug 18 '17 at 04:29
  • Representa**ble**. ...but that would cause all sorts of trouble elsewhere as well. – Nisse Engström Aug 18 '17 at 11:46
  • 1
    First of all ASCII has **7** Bits from **0 to 128** (**0x0 to 0x7F**) after that its called **E**ASCII meaning **Extended**Ascii.that is of **8-Bits** and if you need to print **EASCII** then you need **UTF-8** encoding enabled from **Locale.h** header file. – Haseeb Mir Nov 07 '18 at 20:37
5

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.

MBillock
  • 220
  • 1
  • 6
1

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;
}
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
Sudeep Acharya
  • 226
  • 2
  • 15
1

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

Pankaj Prakash
  • 2,300
  • 30
  • 31
1
#include<stdio.h>
 void main()
{
char a;
scanf("%c",&a);
printf("%d",a);
}
0

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);
    }
}
David R Tribble
  • 11,918
  • 5
  • 42
  • 52
0

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
gvlasov
  • 18,638
  • 21
  • 74
  • 110
0

Simplest approach in printing ASCII values of a given alphabet.

Here is an example :

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