+5

What is the use of atoi function in c

I don't understand the concept please give me an explanation with example

c

7/20/2019 4:34:53 PM

Gayathri Manalan

8 Answers

New Answer

+7

Converts an string to integer.

+6

Gayathri Manalan atoi converts a string to it's equivalent integer value, if the value can be converted. If the string is invalid or if the value to be conveeted is out of range of the int, then the result is undefined. int v = atoi("123"); v now equals 123 int v = atoi("-123"); v now equals -123

+4

~ swim ~ it returns 0 i think if the string is not digit

+3

Gayathri Manalan - or + are valid chars for atoi. atoi("-56") retuns -56 ✳AsterisK✳ undefined behaviour if the number is out of range. But if not a number it returns zero as ~ swim ~ said... atoi("555555555555555") --> undefinded atoi(" -56") --> -56 atoi("fghi") --> 0 :)

+2

It stands for ASCII to Integer.

+1

✳AsterisK✳ Undefined by Standards, compiler implementers are free to do what they want to do, so most return 0.

+1

#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, Int value = %d\n", str, val); strcpy(str, "hello"); val = atoi(str); printf("String value = %s, Int value = %d\n", str, val); return(0); } Run this code using numbers and alphabetic. You will understand better.

+1

In simple words "654" value is not equal to 654. So it converts "654" to 654 value...😉😉😉😉