Hi stefan73,
Signage comes into play in two areas of C.
If a value cannot be negative and might be a very large integer, unsigned is the way to go.
If you're converting (recasting) integer type data, the sign bit of the smaller field can really mess you up.
unsigned int BigValue;
char SmallValue;
SmallValue = 130; /* generates compiler warning */
BigValue = (unsigned int)SmallValue;
You might think that BigValue is set to 130 (0x82). It certainly makes sense. But in fact BigValue is 0xFFFFFF82. In recasting SmallValue, it is extended to 32 bits WITH SIGN EXTENSION. Since it's negative, the 1 bit in the sign position propogates all the way to the top bit in BigValue. The result is that BigValue is probably not the value that you want.
unsigned int BigValue;
unsigned char SmallValue;
SmallValue = 130; /* generates compiler warning */
BigValue = (unsigned int)SmallValue;
This behaves exactly like you think it should.
Good Luck,
Kent
Main Topics
Browse All Topics





by: stefan73Posted on 2004-01-27 at 07:29:52ID: 10210074
1. Signedness is a primarily matter of whether you need negative values or not. Using a sign halves your positive range, of course. But be careful when comparing signed and unsigned values.
2. It's a matter of match. '*' is a pseudo-data type character, it throws away the field, not storing it. So it doesn't need an argument afterwards. [] is the scanset, the set of allowed characters. So "%*[ \t] scans for a sequence of whitespace at line start.