Why doesn't this work?
printf("%d \n\n\n\n", atoi("11110010100"));
it outputs -1774891788... I just want it outputted as it is. It seems to work just fine if the number is a bit smaller.
Why doesn't this work?
printf("%d \n\n\n\n", atoi("11110010100"));
it outputs -1774891788... I just want it outputted as it is. It seems to work just fine if the number is a bit smaller.
atoi
returns an int
. You pass a string which contains a number bigger than what int
(in your implementation) can hold. So, you have an integer overflow.
To print the maximum value an int
can hold, include limits.h
and print INT_MAX
.
int atoi (const char * str) convert string to integer,and the basic signed integer type capable of containing at least the [−32767,+32767] range,
the 11110010100 is bigger than integer storage capability, so you have an overflow.
you can try this method to parse a String to a Double: atof
http://www.lemoda.net/c/string-to-double/