How to convert characters in the C language to ASCII code?
In C language, it is possible to convert letters into ASCII codes using type casting.
Here is an example code:
#include <stdio.h>
int main() {
char c = 'A';
int ascii = (int)c;
printf("字符 %c 对应的ASCII码为 %d\n", c, ascii);
return 0;
}
In the given code, a character variable c is first defined and assigned the value of the letter ‘A’. Next, the character c is converted to an integer type using (int) to obtain its corresponding ASCII code. Finally, the character and its corresponding ASCII code are printed to the console using the printf function.
Running the code above will result in the following output:
字符 A 对应的ASCII码为 65