Sidetrack: Reading Variable Values with scanf() |
scanf(format-string, expr1, expr2, …); |
Converting string into integer
int value = atoi(string); |
Example:
#include <stdio.h> // includes definition of and scanf() #include <stdlib.h> // includes definition of atoi() #define INPUT_STRLEN 20 ... char str[INPUT_STRLEN]; int n; printf("Enter a string: "); scanf("%19s", str); n = atoi(str); printf("You entered: \"%s\". This converts to integer %d.\n", str, n); |
Enter a string: 9024 You entered: "9024". This converts to integer 9024. |