3 ms·
Note: You shouldn't use int, unsigned int, char, short, long. Use int16_t, uint16_t, uint8_t, etc (or their _fast equivalents) from stdint.h. The former's size
by peterwwillis 6y ago
Note: You shouldn't use int, unsigned int, char, short, long. Use int16_t, uint16_t, uint8_t, etc (or their _fast equivalents) from stdint.h.
The former's sizes change based on platform, cpu, and compiler; the latter are fixed-width (or flexible, where _fast may use a larger size if it's faster).
I started brushing up on my C recently and have been collecting these little nuggets: https://gist.github.com/peterwwillis/53cd9d34d8755784e48379047af9f358 https://gist.github.com/peterwwillis/53cd9d34d8755784e483790...
- kccqzy 6y agoNot when you are writing the Linux kernel, when you know exactly which sizes the integers are. Or even when you are writing low-level code on a known platform (e.g. an LP64 platform).
- peterwwillis 6y agoAll the typical sizes depend on the compiler and the flags you provide. If you provide the wrong flags, the sizes of each type may change, but you won't see any warnings about it because it's expected behavior, and now you've got different binaries with different behavior. Or you could use fixed-width types and if the wrong flags get passed (no C99 support) your code just doesn't compile. I just checked the Linux kernel style guide, and they explicitly suggest you can use fixed-width types from C99 when it makes sense. I get that they want a balance for their project, but for general programming, it's just safer to be explicit. https://www.kernel.org/doc/html/v5.1/process/coding-style.html#typedefs https://www.kernel.org/doc/html/v5.1/process/coding-style.ht...