Different results when using atoi () for C on Mac OS X and Ubuntu

Here is the code that I ran on Mac OS X (10.8.1, 64-bit and 10.6.8, 32-bit) and Ubuntu (10.04, 32-bit):

printf("%d\n", atoi("2147483648")); // returns -2147483648 in Mac OS X 10.8.1 and 10.6.8 // returns 2147483647 in Ubuntu 

Here is what I found after some Googling: http://gynvael.coldwind.pl/?id=365

Is there a reason why the output depends on different systems?

+4
source share
1 answer

This is not surprising, because the behavior is not specified by the standard:

There is no standard specification of what happens when the converted value is outside the range of values ​​represented by int.

The strtol function gives you more information in reverse order, since it sets errno to ERANGE when the value is not suitable for long int .

+9
source

Source: https://habr.com/ru/post/1432517/


All Articles