Search code examples
catoi

atoi() in C saving zip codes


Using atoi() while reading from a file and it is dropping the first 0 in some of the zip codes, for example:

int x = atoi("06461");

seems to be saving x = 6461. Is dropping non significant 0's part of the atoi function?


Solution

  • It doesn't drop zero. It stores the number. And as a number (decimal) 06461 and 6461 is exactly the same value. It's up to you how to present the number — with (printf("%05d",zip)) or without (%d in case of printf) leading zero.

    P.S. Note, that c folks are mightily confused by leading zeros, they tend to see numbers as octal then. P.P.S. And I fully support Joachim's comment to your question.