One of the great virtues of C is that this sort of thing is not part of the language ...
Only literally. 7.24.1 in the C programming language spec has these poor parsers.
No way to parse integers in C (2022)
61–70 of 117 posts
Re: No way to parse integers in C (2022)
#62Earlier quoted context omitted.
Could you humor a coding noob--how do you deal with utterly insane inputs like that?
You first ask if you really need to.
Re: No way to parse integers in C (2022)
#63One of the first homework assignments when I learned C back in '83 was after a long lecture on how the string functions are fundamentally broken, and the class introduction to writing C was fixing all of them.
My memory growing up is that making your own C library was basically an inevitable rite of passage for any aspiring programmer.
Re: No way to parse integers in C (2022)
#64Earlier quoted context omitted.
Only literally. 7.24.1 in the C programming language spec has these poor parsers.
Is their misbehavior part of the spec as well? If not, we can always add the correct behavior to the spec and let anyone who implemented a broken version deal with fixing every program compiled using it.
For strtoul and friends, maybe? 7.24.1 is pretty dense, but the key parts are "the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified by base, optionally preceded by a plus or minus sign […] If the correct value is outside the range of representable values […] ULONG_MAX […] is returned".
So the "expected form" allows a minus sign, but then it's clearly "outside the range of representable values" for strtoul to try parsing a negative value. So maybe it should return ULONG_MAX on those.
So arguably a minus sign present could already be treated as an error, and still be standard compliant. Unless I'm misreading.
Re: No way to parse integers in C (2022)
#65As 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 people downvoting you are probably not C programmers and love to hate C.
Re: No way to parse integers in C (2022)
#66I 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…
Could you humor a coding noob--how do you deal with utterly insane inputs like that?
Re: No way to parse integers in C (2022)
#67Re: No way to parse integers in C (2022)
#68Re: No way to parse integers in C (2022)
#69Re: No way to parse integers in C (2022)
#70Earlier quoted context omitted.
My memory growing up is that making your own C library was basically an inevitable rite of passage for any aspiring programmer.
Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.
EDIT: perhaps I should have been clearer; by not having one early on, we now have multiple competing package managers, with no clear winner. Responses prove that point.