- 2
How to convert any string to integer
One character change to integer but how to change string to integer
5 ответов
+ 3
using atoi function is just a bad way to change string to integers.
as Ace had mentioned earlier,
i'll show you my personal way of doing this properly and safely using standard functions. and without remembering all the functions mentioned by Martin Taylor
String to Integer
char string[] = "123";
int num;
sscanf(string, "%d", &num);
printf("num = %d", num);
>>>
num = 123
Integer to String
char string[4];
int num = 456;
sprintf(string, "%d", num);
printf("string = %s", string);
>>>
string = 456
as u can see sscanf and sprintf replicates the behaviour of scanf and printf only that the input and output goes to a variable instead of the user input and console output.
+ 2
Type in a code to convert the string into an integer:
char str_num[] = "123";
int num =
_____
(str_num);
printf("%d", num);
Answer atoi
0
Step by step. Add chars of the string step by step to an integer and multiply the integer by 10 after each step.
0
use atoi to convert str to int. And use search bar to avoid duplicates.
0
Hi