
The first check is to early exit when an error occurs. To showcase this problem (NOT A NEW SUBMISSION) I’ve created a version of my submission that adds two checks. While I’m quite happy with my submission and don’t want to change it, your performance tests skewed toward how the submissions deal with invalid input rather than valid input. NOTE: The best Clang flags to use are: -fsanitize=integer -fno-sanitize=unsigned-integer-overflow

By submitting an entry into this contest you are agreeing to let me use your entry in a subsequent blog post, either as a positive or negative example.

I’ll announce the winner in a few days, and will send the winner a small prize.
#CONVERT STRING TO INT C FREE#
Feel free to leave comments that don’t include code, of course.
#CONVERT STRING TO INT C CODE#
Please mail entries to me, not only to avoid spoiling the fun but also because WordPress loves to mangle C code left in the comments. Your code must not execute undefined behaviors (use a recent Clang to check for this).You may assume that implementation-defined behaviors are the standard ones for the x86 and x86-64 platforms on Linux machines.I’ll probably only test entries using GCC and Clang but even so you are not allowed to use extensions such as GCC’s 128-bit integers You should write conforming C code that works on x86 and x86-64.It is fine to use constants from limits.h.Don’t call library functions your code should perform the conversion itself.No other characters (even whitespace) are permitted. The regular expression above probably isn’t standard either it is simply trying to specify a null-terminated string containing only a signed decimal integer of arbitrary size. I deliberately didn’t name the function atol or strtol since its semantics are slightly nonstandard. * return any value otherwise behavior is undefined * the input is a null-terminated string, set error to 1 and * representable as a long, return the integer otherwise if * if input matches ^-?+\0$ and the resulting integer is The goal of this contest is to submit the nicest (judged by me) implementation in C for this specification:

One apparently simple piece of code that many programmers get wrong in this respect is converting strings to integers. Regular readers will know that I’m obsessed with integer overflows. I’ve created a Github repository containing all submissions and my test harness. UPDATE: As of Saturday March 9, the contest is closed.
