Tae-Sung Shin
Tae-Sung Shin

Reputation: 20620

Difference between atoi and atol in windows

One of clients to whom we provided with source code said that by changing int to long and atoi to atol, they get different result of our program. But as far as I understand, int and long on Windows have the same 4 byte size and the same min/max. In the same analogy, I expected atoi and atol produce same output and with our testing, they do.

Is there any difference between those commands I didn't know?

Upvotes: 1

Views: 1435

Answers (1)

Steve Jessop
Steve Jessop

Reputation: 279385

In non-error cases, the functions are both defined equivalent to

strtol(nptr, (char **)NULL, 10)

the only difference is that atoi casts the return value to int.

There could be different behavior in error cases (when the string represents a value that is out of range of the type), since behavior is undefined for both. But I'd be surprised. Even if atoi and atol aren't implemented by calling strtol, they're probably implemented by the same code or very similar.

Personally I'd ask that the client show me the exact code. Maybe they didn't just replace int -> long and atoi -> atol as they claim. If that is all they changed (but they did so slightly differently from how you assumed when you did your tests), probably they've found the symptom of a bug in your code.

Upvotes: 2

Related Questions