Easy Tutorial
❮ C Function Fabs C Storage Classes ❯

C Library Function - atoi()

C Standard Library - <stdlib.h>

Description

The C library function int atoi(const char *str) converts the string pointed to by str into an integer (type int).

Declaration

Here is the declaration for the atoi() function.

int atoi(const char *str)

Parameters

Return Value

The function returns the converted integer as a long integer. If no valid conversion could be performed, it returns zero.

Example

The following example demonstrates the use 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("String value = %s, Integer value = %d\n", str, val);

   strcpy(str, "tutorialpro.org");
   val = atoi(str);
   printf("String value = %s, Integer value = %d\n", str, val);

   return(0);
}

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

String value = 98993489, Integer value = 98993489
String value = tutorialpro.org, Integer value = 0

C Standard Library - <stdlib.h>

❮ C Function Fabs C Storage Classes ❯