Hi

What are the basic differences between the methods atol() and strtol()?

Regards,
Amit

strtol offers more error handling capability and is safer. atol is easier to use, but is impossible to properly check for errors and exhibits undefined behavior for some erroneous input. The basic difference is that you should use strtol unless your string has already been carefully validated and you're 100% certain that the conversion will succeed with atol.

strtol takes an extra parameter that can be set to what array-element it should convert.
So you could only convert the first 3 chars to a long, or 10 or the middle 5 or ... etc.
atol does not have this functionality.

[edit] And what Narue says is also true, because she's never wrong ;)

Thanks friends for your responses.

strtol offers more error handling capability and is safer. atol is easier to use, but is impossible to properly check for errors and exhibits undefined behavior for some erroneous input.

How can I handle the errors in case of strtol() ?


Amit.

>How can I handle the errors in case of strtol() ?
strtol sets errno, it returns suitable values on error, and one of the parameters is a pointer that marks where the conversion stopped. You can mix and match these three error handling schemes to write a bullet-proof conversion.

Thanx a lot Narue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.