atoi() - Utility Function
How is Utility function "atoi()" used in C++?
How to convert a string to integer value?
is a Utility Function that converts a string into a integer value. The string must contain a integer value, the rest of the string character are terminated by white space, punctuation, characters. If an integer is not found the result may be undefined, but in most scenarios will return 0.
int atoi(const char *str);
int main ()
char arr ;
printf ( "Enter a string with a integer: " );
gets ( arr );
a = atoi ( arr );
printf ("The entered value is:: %d\n", a );
printf ("The bytes occupied is:: %d\n", sizeof(a));
Enter a string with a integer: 3err
The entered value is:: 3
The bytes occupied is:: 4
In the above example atoi() is used to convert the string into integer, by terminating the string "3err" as soon as character "e" is found, then integer 3 is returned.