0

How to convert string to int array?

pls explain it to me.

14th May 2021, 9:39 AM
reza
reza - avatar
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; }
14th May 2021, 10:09 AM
RKK
RKK - avatar
+ 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
14th May 2021, 12:30 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
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.
14th May 2021, 9:51 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar