All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Why C's standard integer parsing functions are broken

By

konmok

11d ago· 8 min readenInsight

Summary

The article critiques the C standard library's integer parsing functions (atol(), strtol()/strtoul(), and sscanf()), arguing they are all fundamentally broken due to undefined behavior, inconsistent error handling, and ambiguous semantics. The author examines edge cases like overflow, invalid inputs, whitespace handling, and locale-dependent behavior, ultimately claiming there is no reliable, portable way to parse integers in C. A postscript update notes that C++'s std::from_chars() appears to be a useful alternative.

Key quotes

· 3 pulled
There are a few ways to attempt to parse a string into a number in the C standard library. They are ALL broken.
What is the correct behavior, anyway?
I'll start by claiming a common sense 'I know it when I see it'.
Snippet from the RSS feed
There are a few ways to attempt to parse a string into a number in the C standard library. They are ALL broken.

You might also wanna read