0

How can I store ASCII of alphabet into int variable?

Suppose I entered a char a; Then how can I store its ASCII into int var? char c; scanf("%c",&c); int var; How can i store Ascii of char in var?

28th Nov 2020, 2:05 PM
Bibek Pant
Bibek Pant - avatar
4 odpowiedzi
+ 5
Martin Taylor thanks a ton for correcting me! I assumed char was uint8_t, because I had once printed a value of type uint8_t and it was printed as a character. I should have dug deeper I guess. My answer has been updated.
28th Nov 2020, 8:20 PM
XXX
XXX - avatar
+ 3
char data type actually stores the ASCII value of the character, which is why can compare and perform arithmetic operators between int and char. Because of that, storing ASCII value of char in int is as easy as casting the char into an int int ascii_value = (int)variable_with_char_type; Moreover, even if you don't cast the char manually, the compiler will automatically. So the statement does the same thing. int ascii_value = variable_with_char_type; Also, please use the search bar before asking a question. https://www.sololearn.com/discuss/1557259/?ref=app https://www.sololearn.com/discuss/550706/?ref=app https://www.sololearn.com/discuss/2010381/?ref=app
28th Nov 2020, 2:20 PM
XXX
XXX - avatar
+ 3
Bibek Pant compiler will automatically covert that Char data value "a" value to int data type value[i.e ASCII value] when you tend to store a char data type value "a" value in int data type.... For example.... char c; scanf("%c",&c); //note that here you should use "&"to specify,in which memory address it want to store int var = c; //here you storing char value in int data type printf("%d",var); //it is ASCII value of inputted character
28th Nov 2020, 2:24 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Thank you guys, its real blessing !
29th Nov 2020, 2:55 AM
Bibek Pant
Bibek Pant - avatar