- 2

How to convert any string to integer

One character change to integer but how to change string to integer

18th Sep 2019, 4:55 PM
Nihal - avatar
5 Answers
+ 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.
20th Sep 2019, 11:25 AM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Type in a code to convert the string into an integer: char str_num[] = "123"; int num = _____ (str_num); printf("%d", num); Answer atoi
25th Feb 2023, 6:30 AM
Jembere Guta
Jembere Guta - avatar
Step by step. Add chars of the string step by step to an integer and multiply the integer by 10 after each step.
18th Sep 2019, 5:31 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
use atoi to convert str to int. And use search bar to avoid duplicates.
19th Sep 2019, 7:20 PM
Manoj - avatar
5th Dec 2023, 10:09 AM
Ankit Kumar
Ankit Kumar - avatar