How to assign the value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to assign the value?

using the value of c assign the value 1 into a char c = '1'; int a; And should be possible for any other value of c to be assigned to a using that method

23rd Jan 2018, 11:08 AM
Jwala Anirudh
Jwala Anirudh - avatar
1 Answer
+ 5
int a = c - '0'; It works because characters are integer like values and their numerical values are the ASCII table (you can Google it if you want but knowing it by heart is not at all mandatory, while it can be useful). Figures from 0 to 9 are represented by following members so '1' - '0' == 1
23rd Jan 2018, 12:06 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar