Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


I 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.


Maybe 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.)


That'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).


Where can I read tips like that about modern C, would you have a book to recommend or a blog to follow? Thank you


I 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://shop.oreilly.com/product/0636920033677.do


I wish I did. I write C for my day job, and pick up most of this from my teammates.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: