Let's say I have the integer 89, how can I print out the ASCII value of 89 which is Y instead of just printing out the number 89?
Assuming you have an integer int value = 89; you can
use printf("%c", value); instead of printf("%d", value);.
or simply putchar(value);
Note however that Y is not the ASCII value of 89. It is the other way around: character Y has an ASCII value of 89. The C language supports different character sets besides ASCII, so 'Y' might not necessarily have the value 89, indeed its value is 232 on IBM Mainframes that still use the EBCDIC character set.
Use the c conversion specifier:
printf("%c\n", (char) 89);
or as pointed out by chqrlie's comment, just simply:
printf("%c\n", 89);
or with context:
char c = 89; /* or int c = 89; */
printf("%hhd is '%c'.\n", c, c);
Referencing the C11 Standard (draft):
7.21.6.1 The
fprintffunction[...]
8 The conversion specifiers and their meanings are:
[...]
c
[...] the
intargument is converted to anunsigned char, and the resulting character is written.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With