C Library Functions -atoi()
C Standard Library - <stdlib.h>
Description
C Library Functionsint atoi(const char *str)Pass the parameterstrThe string pointed to is converted to an integer (of type int).
Declaration
Below is the declaration of the atoi() function.
int atoi(const char *str)
Parameter
- strThe string to be converted to an integer.
Return Value
This function returns the converted long integer; if no valid conversion is performed, it returns zero.
Example
The following example demonstrates the usage of the atoi() function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("字符串值 = %s, 整型值 = %d\n", str, val);
strcpy(str, "example.com");
val = atoi(str);
printf("字符串值 = %s, 整型值 = %d\n", str, val);
return(0);
}
Let us compile and run the above program, which will produce the following result:
字符串值 = 98993489, 整型值 = 98993489 字符串值 = example.com, 整型值 = 0other extensions