Programming tip #6 — never use atoi()
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
