Theme 1 – Locale‑dependent number parsing is error‑prone
Many commenters warn that C’s strtod/sscanf obey the current locale, turning a decimal point into a comma in some environments and breaking code that assumes a dot.
- “C number parsing functions are fundamentally broken, since they are locale‑dependend… one should avoid using them almost always and use
std::from_chars…” – Panzerschrek - “That doesn’t detect overflows and underflows. Also changes based on locale, which is not thread safe…” – lelanthran
- “Anything that isn’t C.UTF‑8 is totally broken anyway.” – tliltocatl
Theme 2 – Detecting conversion errors (overflow, underflow, invalid input) requires extra care
The basic APIs don’t reliably report problems; programmers suggest round‑trip checks, initializing variables to NaN/DBL_MAX, or writing wrappers that validate the result.
- “That doesn’t detect overflows and underflows.” – lelanthran
- “For extra reassurance, initialize foo to NaN or DBL_MAX or something, and verify that it is no longer equal to that value.” – CamperBob2
- A round‑trip snippet that validates the conversion:
c int s2d(double*restrict out,const char*restrict in){ char tmp[99]; int r,q=sscanf(in,"%lf%n",out,&r),s=sprintf(tmp,"%lf.",*out); return q&&!((r>s)||memcmp(in,tmp,r)||tmp[r]>'0'); }– geocar
Theme 3 – C’s error‑handling model (global errno) is awkward; developers prefer functions that return error information directly
The reliance on a global errno variable is criticized as error‑prone and non‑thread‑safe, with many advocating for tuple returns, structs, or sum‑type‑style error handling.
- “errno is a worse mistake than null pointers in general.” – raverbashing
- “[double x, errno_t errno] = strtod(s, nullptr); … Instead, we’re stuck with 'check if the return value is -1 and if it is, check the global variable errno' …” – Joker_vD
- Demonstrates a manual struct return that bundles value and error code. – gucci-on-fleek (code example)
- “Sometimes I wish we could get to a timeline where … sum types/tagged unions …” – muvlon