Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - atoi()

C library function - atoi()

C Standard Library - <stdlib.h> C Standard Library -

Description

C library function *int atoi(const char str) Converts the string pointed to by the argument str The string pointed to by the argument is converted into an integer (of type int).

Declaration

The following is the declaration of the atoi() function.



int atoi(const char *str)

Parameters

Return Value

The function returns the converted long integer; if no valid conversion was performed, it returns zero.

Example

The following example demonstrates the use of the atoi() function.



#include 

#include 

#include 



int main()

{

   int val;

   char str[20];

   

   strcpy(str, "98993489");

   val = atoi(str);

   printf("字符串值 = %s, 整型值 = %d\n", str, val);



   strcpy(str, "runoob.com");

   val = atoi(str);

   printf("字符串值 = %s, 整型值 = %d\n", str, val);



   return(0);

}

Let's compile and run the above program, which will produce the following result:



字符串值 = 98993489, 整型值 = 98993489

字符串值 = runoob.com, 整型值 = 0

C Standard Library - <stdlib.h> C Standard Library -