C programming stdlib.h function - int atoi(const char *str)

www.igi‮ditf‬ea.com

The atoi function in the stdlib.h library of the C programming language is used to convert a string that represents an integer into an int value. The syntax of the atoi function is as follows:

int atoi(const char *str);

Here, the argument str is a pointer to the null-terminated string to be converted.

The atoi function scans the input string pointed to by str, and converts it to an int value. The function stops scanning the input string when it encounters the first character that cannot be part of an integer. The function returns the converted value as an int value.

For example, the following code uses the atoi function to convert a string representing an integer into an int value:

#include <stdio.h>
#include <stdlib.h>

int main() {
   char str[] = "12345";
   int value;
   value = atoi(str);
   printf("The converted value is: %d\n", value);
   return 0;
}

In this example, the input string is the null-terminated string "12345". The atoi function converts this string to the integer value 12345, which is stored in the value variable. The program then uses the printf function to print the converted value. The output of the program is:

The converted value is: 12345

Note that the atoi function does not perform any error checking. If the input string is not a valid integer, the function returns a result, but the result is undefined. It is the responsibility of the caller to ensure that the input string is a valid integer before calling the atoi function.