How to convert char to int in c programming?

How to convert char to int in c?

We can convert char to int by negating ‘0’ (zero) character.

char datatype is represented as ascii values in c programming.

Ascii values are integer values if we negate the ‘0’ character then we get the ascii of that integer digit.

If we want to convert a stringto number then we have to extract the character and then we have to convert it to integer number mathermatically.

Have a look at the below example for converting a character to int value.


Example:

#include<stdio.h>
int main()
{
 char c='1';
 int a=c-'0';
 printf("Converted int is %d",a);
}

Output

	Converted int is 1
output for How to convert char to int in c

People also read