Sunnyhithreddy K
Sunnyhithreddy K

Reputation: 11

How convert a character to integer

Suppose if I have a character let's say char a ='9' and I need to convert it into interger value 9 .How can I do that?.I have tried using inbuilt function atoi().But it is giving error saying you can only pass constant pointer as a arugument.

Upvotes: 1

Views: 71

Answers (1)

Faruk Hossain
Faruk Hossain

Reputation: 1203

It is simple. just subtract '0' from that character.

char a = '9';
int value = a - '0'; // value = 9.

because ascii value of '9' is 57 and '0' is 48.

So actually it becomes

int value = 57 - 48;

That is value = 9.

Upvotes: 4

Related Questions