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
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 |
The return value of atoi() function is an integer value.
Input | Return Value |
---|---|
successful | integer value |
invalid conversion | zero |
#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
#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