How to convert characters to numbers in C language?
In the C language, you can use the atoi() function to convert a string to an integer. For example:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("The converted number is: %d\n", num);
return 0;
}
The result of the operation is:
The converted number is: 12345
Moreover, it is possible to use the sscanf() function for converting characters to numbers, as shown below:
#include <stdio.h>
int main() {
char str[] = "67890";
int num;
sscanf(str, "%d", &num);
printf("The converted number is: %d\n", num);
return 0;
}
The result of the operation is:
The converted number is: 67890