How do I print a char and its equivalent ASCII value in C?
Asked
Active
Viewed 4.1e+01k times
34
-
1So 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 Answers
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
-
5Your 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
-
1Why the above program doesnt print ASCII characters from 127 to 160? – CodeCodeCode May 03 '13 at 12:30
-
6
-
4
-
2Sorry, -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
-
1First 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
-
You don't need to cast to `int`, since the standard type promotions will promote `c` into an `int` automatically. – David R Tribble Sep 24 '09 at 16:18
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
-
ASCII has 7 bits so **0 to 127** after thats is system defined **EASCII**. – Haseeb Mir Nov 07 '18 at 20:34
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;
}

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

Shivam Panchbhai
- 89
- 2
- 11
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
-
-
@NisseEngström - Code corrected, for the truly pedantic. Yes, it's always a good idea to use `unsigned char[]` for text buffers. – David R Tribble Sep 25 '17 at 16:08
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

syamantak82
- 11
- 1
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

Python-Baby
- 23
- 2