News for hackers. Stuff that matters.
Re:How to C in 2016 by caf on Friday January 08, 2016 @08:05AM (#10864549)
You are right, and this advice is wrong.

`int` is at least 16 bits, `long` is at least 32 bits, `long long` is at least 64 bits, and `size_t` is big enough to hold the size of the largest object you can allocate - and in most cases that is all you need to know.

The exact width types can be useful if you're marshalling data to and from an exact-width format, like a network protocol or file format.

Re:How to C in 2016 by Nursie on Friday January 08, 2016 @08:49AM (#10864758)
I disagree.

You should know the range of values that you are expecting to be holding in an integer, and choose the type accordingly.

Re:How to C in 2016 by caf on Friday January 08, 2016 @05:43PM (#10868872)
That does not contradict what I said. When choosing the type, all you need is a type that has at least the range you require, and the basic types give guaranteed minimum ranges.

The only advantage to choosing an exactly-N-bit type over an at-least-N-bit type is possibly saving some space in very large allocations. In most cases this won't be important - and when it is, the mininmum-width types from stdint.h (eg int_least32_t) are more apropos than the exact-width types.

The disadvantage is the impedance mis-match with all the existing libraries of code using the basic types.