Search code examples
cprintfatoi

Why does printf convert large numbers?


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.


Solution

  • 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.