Atoi() is the C runtime library function for converting the ASCII representation of a number to an integer


Strtol() is the C runtime library function for converting the text representation of a number to a long integer

Errno handling sscanf

Quality Example

"For instance strtol is better than atoi and you should be checking each time whether strtok returns null"

from question "Putting values into a struct from a file"

"You can also look into strtol which is better than using atoi in terms of error checking"

from question "Int to char array as a function returning an array of char the simple way"

"You need to error check strtol and ensure there are as many passed before using them -- strtol is better than atoi as helps detect errors"

from question "Passing argument from main to function in C"

"Use strtol it does better error reporting than atoi"

from question "Getting the first value from this string and convert it to an integer"

"If you want to accept a number rather than a digit and only a number strtol works much better than atoi as it allows you to check for failures"

from question "C Using isdigit to check if optarg is a digit"

"All have more or less cumbersome and non-obvious error checking involving errno strtol is way much better than atoi in any case so avoid using atoi"

from question "Which is preferd use istrstream or strtol/strtod for reading number from string"

"Strtol is better than atoi with better error handling"

from question "String conversion to int"

Better option

"Besides strtol is a better option than atoi as strtol can handle failures better"

from question "In C: I want to take a mix of characters and convert them to lower case"

Better error

"I would recommend strtol which provides better error handling than atoi or sscanf"

from question "C: How to convert a string of ints into actual ints and store them in an array?"

Much better

"The c function strtol is much better make it a habit to prefer that one to atoi"

from question "How do I convert a char* to an int?"


Quality Example
Obviously better

"You can also use strtol which is obviously better than atoi"

from question "Runtime error using atoi() in C"


"I start off calling gets to fill the buffer then using strtol to convert the human-readable text in buffer to an actual computer int value - note that strtol is more powerful than atoi and uses long int instead of int as its declared type"

from question "Having trouble acquiring Int from "gets(*buffer)""

More flexible

"I understand that strtol and strtof are preferred to atoi atof since the former detect errors and also strtol is much more flexible than atoi when it comes to non-base-10"

from question "Why does OSX document atoi/atof as not being threadsafe?"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0