3 ms·
Newsflash, the C language has conversions. You can assign a long or long long to your int32_t.
by foxfluff 5y ago
Newsflash, the C language has conversions. You can assign a long or long long to your int32_t.
- ihattendorf 5y agoGreat, and what happens when you accidentally assign something greater than 2^31 to an int32_t when using strtol? You won't benefit from a range error if LONG_MAX is 2^63, and now you have to make sure to handle any implementation defined behavior.
- foxfluff 5y agoThat's true. If the range you're interested in is not the same as the range of long or long long, you'll have to check the range yourself before you go on and use the value. No ifdefs required. If you're not happy doing it yourself, I do recommend strtonum or any of the alternatives that allow you to explicitly specify the range you're interested in. I don't see the point in littering the standard library with functions having hard-coded range for every range you might be interested in.
- marcosdumay 5y agoAnd get undefined behavior where the compiler can do anything it wants...
- cwzwarich 5y agoUnlike other cases of signed overflow, you actually don’t get UB on integer-to-integer conversions. You still have a bug in your code, though.