0
How to convert string to int array?
pls explain it to me.
3 Answers
+ 1
#include <stdio.h>
int main() {
char num[3] = "123";
int number[3];
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
number[i] = (num[i] - '0');
}
// printing
for(int i = 0; i < sizeof(number)/sizeof(number[0]); i++) {
printf("%d ", number[i]);
}
return 0;
}
+ 1
what does it mean to convert? in C there are only numeric types. Basically char is an unsigned byte, and the character literals are stored as the integer value of the ASCII table
#include <stdio.h>
int main() {
char c='0';
printf("%c %d",c,c);
return 0;
}
Output
0 48
0
reza
1.The easiest way to do this is by using the " stoi() "
2.We can convert a char array to int using the " atoi() " function. defined in cstdlib.