Archive

Archive for June, 2016

C type system strikes back

9.06.2016 Leave a comment

Today is one of those days when C++ surprises me. Here is a snippet:

#include <stdint.h>
#include <stdio.h>

int main()
{
    const int64_t a = 3;
    if (a < -0x80000000)
        printf("%lld is negative\n", a);
    return 0;
}

This time the surprise comes from the intricacies of the C type system.

Hint: -0x80000000 is mathematically INT_MIN.

In case you’re wondering, it is not about improving the above snippet. It is about actually seeing a piece of code like this in the wild. It looks correct. But it is incorrect.

Advertisements
Categories: Other