14 ms·
A little C tip I learned from hard experience NEVER, EVER, NOT IN A MILLION YEARS use a signed int/char etc, unless you are 200% certain you're doing the right
by raverbashing 14y ago
A little C tip I learned from hard experience
NEVER, EVER, NOT IN A MILLION YEARS use a signed int/char etc, unless you are 200% certain you're doing the right thing (that is, it's for something specific you need it)
You WILL have problems, period.
"Oh it's just a matter of knowing the C spec" then please go ahead as I grab the popcorn.
- Roboprog 14y agoThere's a certain aspect of "functions have domains, not just ranges" at work here as well -- e.g. - restricting the (math) tan() function to the domain of -90 to 90 degrees (exclusive), unless you really get off on watching it cycle over madness. If you are going to be playing around the edges of something, it behooves you to put some kind of pre-condition in with an assert of similar mechanism. In fairness, I guess a function like this is a good example of why you should put in preconditions, as well as a good demonstration that "not all the world is a VAX" (nor MS C 7, nor GCC version N) :-)
- timbre 14y agoI'm rusty on C, and confused. What should I use instead of signed int?
- rcfox 14y agounsigned int :)
- bvdbijl 14y agoand for negative numbers?
- rcfox 14y agoIn that case, you can be pretty certain that you need a signed integer.
- esrauch 14y agoWhat if you only need to store negative integers?
- duaneb 14y agoIsn't that the same situation as only storing signed integers? The arithmetic is just a little different.
- ralph 14y agoNo, you get room for one more negative int. x = x < 0 ? -x : x; is a common buggy abs(), e.g. when printing an integer. One should test if it's negative, perhaps print the '-', and then ensure it's negative for the rest of the per-digit code.
- barrkel 14y agoI've seen more problems from unsigned ints than signed ints (in particular, people doing things like comparing with n-1 in loop boundary conditions). There's a reason Java, C# etc. default to signed integers. Unsigned chars, I have no quibble (and Java, C#, use an unsigned byte here).
- rcfox 14y agoUnsigned integer overflow has defined behaviour in C, while signed overflow doesn't. Is it really better to protect people from a simple logical error by exposing them to possible undefined behaviour? With signed integers, you'll run into the same problem with comparing to n+1 at INT_MAX or n-1 at INT_MIN.
- barrkel 14y ago0 is a really common value for an integer variable in programs. INT_MAX and INT_MIN are not. It's just my experience. Don't get too wound up about it ;)
- raverbashing 14y ago"(in particular, people doing things like comparing with n-1 in loop boundary conditions" There's your problem: that's like saying cars are more dangerous than motorcycles because your finger can get squeezed by the door. "There's a reason Java, C# etc. default to signed integer" Legacy? And in Java/C#, and you usually use ints, not so much chars, shorts, etc and casts are probably more picky I stand by my point, you should only use signed if you know what you're doing and for a specific use only (like math)
- barrkel 14y agoSigned arithmetic is generally only problematic when you hit the upper or lower bound. The right answer is almost never to use unsigned; instead, it's to use a wider signed type. It's far too easy to get things wrong when you add unsigned integers into the mix; ever compare a size_t with a ptrdiff_t? Comes up all the time when you're working with resizable buffers, arrays, etc. And no, Java did not choose signed by default because of legacy. http://www.gotw.ca/publications/c_family_interview.htm http://www.gotw.ca/publications/c_family_interview.htm: "Gosling: For me as a language designer, which I don't really count myself as these days, what "simple" really ended up meaning was could I expect J. Random Developer to hold the spec in his head. That definition says that, for instance, Java isn't -- and in fact a lot of these languages end up with a lot of corner cases, things that nobody really understands. Quiz any C developer about unsigned, and pretty soon you discover that almost no C developers actually understand what goes on with unsigned, what unsigned arithmetic is. Things like that made C complex. The language part of Java is, I think, pretty simple. The libraries you have to look up." Unsigned is useful in a handful of situations: when on a 32-bit machine dealing with >2GB of address space; bit twiddling where you don't want any sign extension interference; and hardware / network / file protocols and formats where things are defined as unsigned quantities. But most of the time, it's more trouble than it's worth.
- ajross 14y ago"For something specific you need it" meaning ... a negative number, like an array or memory address offset? I mean, sure, I agree that you should be doing anything sensitive to 2's complement behavior on unsigned quantities. And if you know the values are positive-definite, unsigned has much cleaner behavior. And I'd even entertain the argument that unsigned should have been the default in the language spec and that signed quantities are the ones that should have specially declared types. But... your advice as written is just insane. They are real, and required routinely. You can't just apply this as a "for dummies" rule without essentially ruling out half of the code they'll need to write.
- raverbashing 14y ago". a negative number, like an array or memory address offset? " A negative number yes, but not really a memory offset (you shouldn't mix negative numbers and memory offset, really) But yeah, if you're doing "math" go for it, but it's on rare occasions where you need negative numbers (subtraction yes) The most common case I remember may be sound samples, where you have signed chars. For all other cases you would be using floating point or decimal numbers
- Natsu 14y ago> For all other cases you would be using floating point or decimal numbers If I'm trying to avoid mathematical anomalies, floating point is not what I would run to... "Equal" is a matter of degree, you have to be careful with anything near zero and you can't carelessly mix together numbers that are a few orders of magnitude different than each other.
- raverbashing 14y agoAbsolutely But for most of "math" you would go for floating point. You won't reinvent some fixed point math using integers just because...
- tedunangst 14y agosigned usually promotes to unsigned in nice ways, such that if you really want to store -1 in your unsigned, it will just work. I've found using all unsigned types, with the occasional signed value jammed into them, is less error prone than mixing and matching types throughout the program. ymmv, and of course, here be the odd dragon or two.
- regularfry 14y agoIt's not just the C spec you've got to watch. I saw a wonderful bug last week where the author hadn't spotted that write(2) returned a ssize_t, not a size_t (or didn't appreciate the difference), so was gleefully checking an unsigned variable for a -1 error result.
- nitrogen 14y agoHow did the bug manifest itself? You can store 0xffffffff(ffffffff) in a 32(64)-bit unsigned int, or a 32(64)-bit signed int. In the one case you'll get UINT_MAX, -1 in the other, but they should compare as equal. If you have -Wextra turned on, gcc will give a warning, though. Here's some sample C code tested on a 32-bit Linux system: #include <stdio.h> int main(int argc, char *argv[]) { unsigned int val1 = 0xffffffff; printf("val1 == -1: %d\n", val1 == -1); return 0; } The result: val1 == -1: 1
- mikeash 14y agoIt works for == -1, but not for < 0, which is a common way to check for error returns from UNIX calls. Any decent compiler should warn that such a check is always false, but people don't always pay attention to that stuff....
- regularfry 14y agoGot it in one :-)
- nitrogen 14y agoC is my absolute favorite language, and as such, I learned a long time ago to pay very close attention to compiler warnings and Valgrind memory errors.
- mikeash 14y agoSadly, a lot of people have never learned this valuable lesson, and happily build their code with hundreds or even thousands of warnings.
- cpeterso 14y agoGoogle's C++ style guide strongly recommends using signed ints over unsigned ints: https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Integer_Types https://google-styleguide.googlecode.com/svn/trunk/cppguide....
- duaneb 14y agoActually, a colleague recently convinced me to start using signed ints for, e.g., for loops instead of unsigned ints. His reasoning was that if you're overflowing with signed integers, you'll probably overflow with unsigned integers too (we work with very large numbers), but it's easier to notice if you have a negative number rather than silently wrapping around to a still-valid value.
- cperciva 14y ago99% of the time, you should not be using any sort of int for a loop variable. Loop variables are almost always array indexes, and array indexes should be size_t.