3 ms·
Is it your experience that there are many projects where you can reliably say some variable will never need arithmetic behavior? I don't have 16 years of exper
by bee_rider 4y ago
Is it your experience that there are many projects where you can reliably say some variable will never need arithmetic behavior?
I don't have 16 years of experience writing C, but
> If you do not need arithmetic behavior, do not use signed integer, use unsigned
does seem in a roundabout way to match the advice that I've gotten from other folks -- usually stick to signed (because you never know if somebody is going to want to use an integer in, say, a downwards loop). Your comment just seems to highlight the less usual case, where you can be sure that nobody will ever need that arithmetic behavior... maybe it depends on the type of applications, though.
- up2isomorphism 4y agoIt always depends on the area of focus. You can also consistently choose the default int type to signed while being fully aware of you lose half range but gain signed integer arithmetic. For me I am mostly doing system and networking so I tends to to unsigned. The key is to choose a consistent default while aware of that you are in a non-intuitive zone when that default type puts you in.
- me_me_me 4y agoFor me using uint8_t and family makes things simpler (working predominately with packets) It is a container for X bits and thats it, its easy to understand overflow and corner case behaviors. I rarely find good reason to use signed ints, so only when I need them i worry about sign handling. > because you never know if somebody is going to want to use an integer in, say, a downwards loop Why risk a possible bug, for a hypothetical case of someone wanting signed var? If they really want to use signed vars they can handle it themselves.
- bee_rider 4y agoYeah. My context was doing linear algebra stuff. It is apparently a known mistake to say "well we aren't going to have to describe negative positions for matrix elements, so why not make the integer unsigned?" but there are lots of cases where you want a negative offset for whatever reason. ("I want to stop right before the diagonal"). I can definitely see why you might not ever need a sign in networking stuff.