Live data from Hacker News

No way to parse integers in C (2022)

blog.habets.se

31–40 of 117 posts

Re: No way to parse integers in C (2022)

#31

Another case many integer parsing functions get wrong is that they interpret a leading 0 as an octal indicator. That should be opt-in via a flag, if it needs to be supported at all. Unix file permissions are the only deliberate use of octal I've ever seen.

It used to be much more common. In the 70s there was a lot of collective hesitance to use hex with its strange letter digits. Octal was the compact representation of choice.

Re: No way to parse integers in C (2022)

#32

Earlier quoted context omitted.

What if the number you want to return just happens to be the value of ERROR? You need an error flag that can't be represented as an int, but then C wouldn't let you return it from a function that only returns "int". It is why some languages throw exceptions and why databases have the special "null" value.

I don't use C enough to know what the convention is for throwing an error when the function can return a number anyway. You'd have to ask someone else

In C, errors are usually indicated by a negative return value constant, crashing the program with abort, or setting the errno global (thread-local, but whatever) and expecting callers to check it. Sometimes multiple of those.

Re: No way to parse integers in C (2022)

#33

Why not look at how other languages attack this? e.g. how does "42".parse() work in rust? Edit: https://doc.rust-lang.org/src/core/num/mod.rs.html#1537 interesting! It boils down to this pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result { use self::IntErrorKind::*; use self::ParseIntError as PIE; // guard: radix must be 2..=36 if 2 > radix || radix > 36 { from_ascii_radix_panic(radix); } if src.is_empty…

It's not an overwhelming hard problem. There are some issues with radix signaling, exponent notation, decimal points being allowed or not, and group separators that make parsing numbers incredibly irritating. So you usually don't want to do it yourself.

But it's not hard at all. It's not even as full of small issues that you can't handle the load, like dates. It's just annoying as hell.

The problem is exclusive to C and C++. It's created by the several rounds of standardization of broken behavior.

Re: No way to parse integers in C (2022)

#34
post #2

I thought it was pretty well known that everything related to strings in C stdlib (including all str... functions) is bad. You just need to bring in your own string library.

Not just the string-related functions. If you want robust error checking, re-entrant code, and bounds checking performed in library functions (instead of performing bespoke validations all across your code base), you have some work to do. Yes, some improvements have been tacked on over the years, but many problems ("current locale", for one) remain endemic.

In my experience, the worst part of the C standard library is not its existence, but the fact that so many developers insist on slavishly using it directly, instead of safer wrappers.

Re: No way to parse integers in C (2022)

#35

I wasn't in this class myself, but one prof at my alma mater started his "Programming 201" class with the simplest assignment: write a C program that accepts two integers from the user and prints their sum. It actually was the only assignment for the rest of the semester, since he has a test suite that would humiliate the students gently at first, but would ultimately pipe a billion nines into stdin as the first argu…

Perfect is the enemy of good.

Re: No way to parse integers in C (2022)

#36
post #11

Earlier quoted context omitted.

Exactly. A wrapper that handles all of the edge cases properly and gives proper reporting just gets added to your own library of functions and the devs get used to using it. Much like the code for abstract data types like lists/hashmaps/etc which neither C nor the standard libraries provide. Bonus points for having bespoke linting rules to point out the use of known “bad” functions. In one old project we went through…

> like lists/hashmaps/etc which neither C nor the standard libraries provide There is a hashmap implementation though: https://man7.org/linux/man-pages/man3/hsearch.3.html

“One hashmap for your entire program” is not generally what people mean when they want a hashmap.

Re: No way to parse integers in C (2022)

#38
post #16

As a C programmer, I find this kind of bad faith article very irritating. Yes, the standard library is bad. This is by far the worst part of the C legacy. But it is not that hard to write your own. String functions like this are not difficult at all, and you can use better naming and semantics, write faster code etc. C is not the C standard library, ffs.

The thing I find irritating is all the folks who say C is broken because it’s not a write once run anywhere language like JavaScript or python. Part of the deal has always been that the programmer needs to understand the target platform and the target compiler’s behavior.

isn't the whole point of C that it's portable assembly though? needing to understand the target platform/compiler's behavior to write correct code seems to cut against that claim quite a bit.

Re: No way to parse integers in C (2022)

#39

I wasn't in this class myself, but one prof at my alma mater started his "Programming 201" class with the simplest assignment: write a C program that accepts two integers from the user and prints their sum. It actually was the only assignment for the rest of the semester, since he has a test suite that would humiliate the students gently at first, but would ultimately pipe a billion nines into stdin as the first argu…

Would be fun to write a program that arranges to send the input into dc(1) and just outsource the whole problem to Ken or Rob or whoever wrote it. :)

Re: No way to parse integers in C (2022)

#40
post #15

Cant you just: for(int i = 0; i = 0) { ret = ret * 10 + characters[i] - 48; } else { return ERROR; } } return ret; Adjust until it actually works, but you get the picture.

this wouldn't catch overflow or underflow errors, nor does it allow non-base-10 numbers, nor does it handle negative numbers. and writing your own parser is a failure case by op's logic. they are complaining about the builtin parsing functions. the author admits you can parse signed integers in their second example, but for unsigned, they don't like seem to like that unsigned parsing will accept negative numbers and…

I think "output" is just supposed to be a human-readable version of "output raw". So the line in the table where "output raw" is 2 but "output" is 1 looks like a mistake. It's repeated in the table for sscanf().
Post reply on HN