4 ms·
I don't see any reason in modern C to use int/short/long over explicit int32_t/int8_t/int64_t other than they're shorter to type.
by xxpor 8y ago
I don't see any reason in modern C to use int/short/long over explicit int32_t/int8_t/int64_t other than they're shorter to type.
- berti 8y agoI tend to use int for loops (where I know there will always be less than 2^16 iterations!), return codes etc. purely because it allows the compiler to pick a fast word size everywhere (e.g. if somebody compiles your code for AVR, and your loops are all int32_t, your API returns int32_t, they're gonna have a worse time). Otherwise, I fully agree.
- skissane 8y agoMaybe you could use int_fast16_t? (That means, pick the fastest signed integer type which is at least 16 bits.) Of course, "int" is less typing than "int_fast16_t". But, int_fast16_t possibly makes it more obvious why you've chosen the type you did (i.e. you only need 16-bits, but aren't relying on any overflow so a bigger type can freely be substituted if that gives better performance.)
- berti 8y agoThat's certainly a valid approach, but I've never actually seen those used in practice. I guess, like me, people are lazy and prefer typing just int, which is guaranteed to be at least 16 bits, and is generally the fastest int type (well, excepting some 8-bit archs.. :P).
- karolist 8y agoWhere can I read tips like that about modern C, would you have a book to recommend or a blog to follow? Thank you
- xxpor 8y agoI wish I did. I write C for my day job, and pick up most of this from my teammates.
- macintux 8y agoI can't speak to the utility of either, but a couple of resources I've found recently: http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf http://shop.oreilly.com/product/0636920033677.do http://shop.oreilly.com/product/0636920033677.do