Programming tip #6 — never use atoi()

Kosta Zertsekel
Sep 1, 2018 · 1 min read

Actually, atoi() is a member of a family of functions defined in C standard library:

  • atoi()
  • atol()
  • atoll()

So, we should never use any of these atoi-family functions. None of them!

You can look up these functions using Linux man pages:

man atoi

It is even written in Linux man pages that the behavior of atoi() is the same as as of strtol()except detecting errors. What a fun!!! So, instead of atoi() we need to use:

strtol(nptr, NULL, 10);

Actually, there is even a rule MISRA-C:2012 rule 21.7 defined by the venerable MISRA-C coding guideline.

Also, this stackoverflow question has you covered.

Stay tuned,

— Kosta Z

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade