+ 5
Manav Roy Just assign char value to int data type int abc = 'c'; if you want to convert char c = '4' to int you can also do this: char a = '8'; int n = a - '0'; cout << n; Subtracting the decimal ASCII value of 0 (zero) from decimal ASCII value of entered digit into char format results to corresponding digit. 
18th Feb 2022, 9:36 AM
A͢J
A͢J - avatar
+ 3
char n = '4'; int x = n - '0'; cout << x << endl; x will become 4 when printed. why does this work? because ascii values of char '4' is 52, and ascii of char '0' is 48. so 52 - 48 = 4
19th Feb 2022, 7:09 AM
Shen Bapiro
Shen Bapiro - avatar
+ 2
David Dolejší You just answered based on others answer. I was the first person who had given answer first so I didn't recognise what he wanted. If there was example like this char a = '4' then my answer might be different but there was no any example so I had given thet Answer. I hope now you are clear. Manav Roy You can also convert like this: char a = '8'; int n = a - '0'; cout << n; Subtracting the decimal ASCII value of 0 (zero) from decimal ASCII value of entered digit into char format results to corresponding digit.
18th Feb 2022, 2:39 PM
A͢J
A͢J - avatar
+ 2
Manav Roy atoi converts C-style strings to numbers while stoi converts strings of type std::array to numbers. If the string you pass in isn't actually convertible to a number, atoi will silently fail, but stoi will throw an exception. I've attached an example below. And, stoi should be preferred to atoi in C++ https://code.sololearn.com/cqN8ci9E7r0w/?ref=app
19th Feb 2022, 4:48 AM
Rishi
Rishi - avatar
+ 2
Manav Roy C style strings are ones that we use a character pointer to store and terminate with a '\0'. For example, char* str="yo" is a C style string with length 3 bytes. Atoi only accepts string parameters, so you can't use char with it.
19th Feb 2022, 5:46 AM
Rishi
Rishi - avatar
+ 1
David Dolejší He didn't not mentioned anything in question. He just asked how to convert char to int. So suggested him that.
18th Feb 2022, 2:30 PM
A͢J
A͢J - avatar
+ 1
Manav Roy you need to understand that what differentiates between a char and an int is only the representation of data that is presented when it is being outputted. for example when a char and int is stored the value 97. both of them are storing the same value. the difference is that when you output int, it outputs the stored value in numbers which is 97. while char would instead look up the ascii table to output what the represented char of the value 97 is, which of course would be 'a'.
19th Feb 2022, 2:37 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Manav Roy You can cast a valid string to int and vice versa...If error occurs you can handle the exception like https://www.udacity.com/blog/2021/07/cpp-try-and-catch-statements-explained.html
20th Feb 2022, 12:36 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
char c = '5'; c ASCII value is 53, so just subtract '0' i.e. ASCII of '0' 48 from c to get the difference which is int 5
19th Feb 2022, 7:40 AM
saurabh
saurabh - avatar
0
Manav Roy 52 is ASCII value of 4 48 is ASCII value of 0 So when you do '4' - '0' = 52 - 48 = 4 which is int value of char '4' Here is the list of all ASCII values https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
19th Feb 2022, 9:26 AM
A͢J
A͢J - avatar