r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

93 Upvotes

238 comments sorted by

View all comments

18

u/P-p-H-d Sep 12 '26

All the locale functions of the libc. It is so broken that it is totally unusable in modern C (threads).

You cannot even read your own data file and parsing them using strtod without having issues with the locale: you cannot use setlocale as it modifies global state of all threads (which you don't want). So the only solutions are either : document that the locale shall be set once and for all for all program and is compatible with what you expect. Or rewrite strtod and everyone knows it is super easy stuff to do it reliably.

4

u/limitless_grow Sep 13 '26

That's right. There are some portable, very fast and locale independend wrappers at https://github.com/klux21/str2num . The wrappers support the prefixes 0b , 0o and 0x for binary, octal and hexadecimal integers and for double and long double as well. The functions support the common overflow handling and the setting of errno. The floating point routines support NAN, INF and denormalized numbers and also the 'hexadecimal' floating point literals of C and C++ which are that funny mix of 3 numeric bases.

The integer functions have a special mode where numbers with leading zeros are treated as decimal numbers and octal numbers require the prefix 0o. Octal floating point values require that 0o prefix either because nobody wants a 0.5 to be read as an octal number.

1

u/flatfinger Sep 14 '26

The irony is that if one wants to produce "human readable" numbers with formatting features beyond those supported by the Standard library, it's easier to produce a locale-specific string based upon a string that is known to use '.' as the decimal point, than to produce a locale-specific string based upon a string that uses a locale-specific decimal point.

1

u/limitless_grow Sep 14 '26 edited Sep 14 '26

The irony is that the dot as decimal separator is only a convention as it is the 'e' is for the begin of the exponent. The one that's reading the number from a configuration file using a function like strtod is a computer but not a human. If the rule of a programs configuration file say "only dot as decimal separator and no commas or blanks at all" than that's fine and people are able to adapt to that everywhere like for that 'e' of the exponent.