C atoi()

The atoi() function is defined in the stdlib.h header file. It helps to convert a given string argument(str) into an integer value ie type int.


int atoi(const char *str); #where str should be a string

 

atoi() Parameters: 

The atoi()function takes a single parameter. In C language typecasting is supported by stdlib.h headerfile.

Parameter Description Required / Optional
str  the string having the representation of a floating-point number Required

atoi() Return Value

The return value of atoi() function is an integer value.

Input Return Value
successful integer value
invalid conversion zero

Examples of atoi() 

Example 1: Working of atoi() function in C?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   int x;
   char st[20];
   
   strcpy(st, "23333444");
   x = atoi(st);
   printf("String value is %s, Int value = %d\n", st, x);

   strcpy(st, "C programming");
   x = atoi(st);
   printf("String value is %s, Int value = %d\n", st, x);

   return(0);
}

Output:


String value is 23333444, Int value is 23333444
String value is C programming, Int value is 0

Example 2: How atoi()() works in C?


#include <stdio.h>

int main (){
    char arr[10] = "100";
    int v = atoi(arr);
    printf("Value is %d\n", v);
    return 0;
}

Output:



Value is 100