I need to take a keyboard input letter, and save that letter's decimal value as an integer. How can I do that with scanf ?
I just figured out myself.
if I want to store the decimal value of 'z'
I can just do int value='z'-'a'+ 97
This is what I wanted to know.
This code reads a char from keyboard ( stdin
) using scanf()
, stores it in byte-sized variable c
of type char
, and then prints its ASCII decimal value as an int
to stdout
:
char c;
scanf("%c", &c);
printf("%d", c);
If you want to output the ASCII value, simply printf()
with the %d
format string.
char ch = 'a';
printf("%d", a); // should be 97
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.