# Convert char to int in C and C++

How do I convert a `char` to an `int` in C and C++?

## Solutions

Depends on what you want to do:

to read the value as an ascii code, you can write

``````char a = 'a';
int ia = (int)a;
/* note that the int cast is not necessary -- int ia = a would suffice */
``````

to convert the character `'0' -> 0`, `'1' -> 1`, etc, you can write

``````char a = '4';
int ia = a - '0';
/* check here if ia is bounded by 0 and 9 */
``````

Explanation:
`a - '0'` is equivalent to `((int)a) - ((int)'0')`, which means the ascii values of the characters are subtracted from each other. Since `0` comes directly before `1` in the ascii table (and so on until `9`), the difference between the two gives the number that the character `a` represents.

Well, in ASCII code, the numbers (digits) start from 48. All you need to do is:

``````int x = (int)character - 48;
``````

Or, since the character '0' has the ASCII code of 48, you can just write:

``````int x = character - '0';  // The (int) cast is not necessary.
``````

C and C++ always promote types to at least `int`. Furthermore character literals are of type `int` in C and `char` in C++.

You can convert a `char` type simply by assigning to an `int`.

``````char c = 'a'; // narrowing on C
int a = c;
``````
Tags: C++ / C / Gcc

## Similar questions

Convert char String to an int but not possible to convert a char from an char string to an int?
Is there a solution to convert an char "slice" of an char string into an int? Short description: User inputs a string with values for example: 1, 2 3 4 ,5 Iam formating that string to 12345 With each number i want to continue to work with the index of an array.
Correct sscanf() prototype, "int sscanf ( const char * s,const char * format, ...);" or int sscanf (char * s,const char * format, ...);?
Here is the prototype for the sscanf() function as described in the cplusplusreference(link) : But I find something fishy about it.Not only does the type of first argument differ from many other string library functions like strcpy()(1) and strcat()(2) (const char* vs char*),but it also seems odd how can we make the array pointed by the first argum...
What's the difference between int main(int argc, char* argv[]) and int main(int argc, char** argv)
What's the difference between int main(int argc, char* argv[]) and int main(int argc, char** argv) I don't see any difference. And, which is better to use?
What is this error? cannot convert 'char**' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)
I am korean. And i made "bank manage program code" for my homework. But thers is problem in my code.(please ignore korea) When i run my code, the error cannot convert 'char**' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)' happened. I don't know about this problem. So I add * to that part. But the problem doesn't solve. Is there ...
The difference between int(*function)(int,int) and int*function(int,int)
I am studying pointers of C and from the book of Deitel I don't get the difference between int(*function)(int,int) and int*function(int,int) when the function is expressed.
are C++ int, int[], float, and especially char *, char[], char are same as these of C. I believe they are premitive so are they same?
There are some datatypes to contain data (in memory I do think). So I like to know these types, are same in C and C++. If they are same then can I use memory functions and string functions and int, float, decimal,binary,etc functions available in C like memset,memcpy,memcmp,etc. and string functions like strcmp, strstr, strlen,etc. in cpp file and ...