C - is any use of unsigned int just terrible coding practice? -


are there examples of legitimately use of unsigned data (i.e. unsigned int) or should use of unsigned data types considered bad coding practice relic of resource impaired platforms 1970s , 1980s?

consider this:

int main () {     unsigned int = 5;   /*or uint32_t if prefer*/     unsigned int b = 8     unsigned int c = - b; // can't store subtraction result                             // own data type!      float f;    // iso didn't bother make signed version of float.     double d;   // iso didn't bother make signed version of double.      // size_t unsigned integer, length varies     // (4 bytes on 32 bit platforms typically, 8 on 64 bit, ...)     size_t size1 = 100;      size_t size2 = 200;      // what's ssize_t -- it's signed size_t because size_t can't store subtractions.     // ssize_t bad idea correct bad idea of size_t being unsigned     ssize_t size3 = size1 - size3;      // unsigned operations don't overflow/underflow     size_t  size4 = size1 - size3; // don't underflow, wrap.                                     // means unsigned isn't                                     // use pseudo data "validation" } 

additionally, c definition of memset, example:

void * memset ( void * ptr, int value, size_t num ); 

memset's value argument unsigned char, great number of functions convert unsigned char int dodge use of unsigned data type or how printf promotes char int.

and in scenario unsigned being used greater range (i.e. 32-bit 4gb) more sign wrong datatype being used , either int64 variant or double should used store value begin with.

there has legitimate use of unsigned can't think of scenario. scenario should unsigned types used?

the rule of thumb in case simple: use unsigned types represent unsigned values, use signed type represent signed values. so, in reality opposite: of time gratuitous use of signed types terrible coding practice. i'd go far of integer types in code supposed unsigned. of course, actual ratio depend on application area, combinatorial problems , related domains: unsigned, unsigned , unsigned.

your above example wrap-around behavior demonstrates typical newbie coding error. , in essence no different popular

double d = 1/2; 

followed "why d not 0.5?".

note in domain of integral calculations unsigned types typically more efficient signed ones (c rounding rules division different typical machine-supported ones , make negative impact on performance of signed types). in mixed integer-floating point calculations signed integer types might have edge (fpu instruction set typically supports signed integers directly, not unsigned ones).

great number of functions convert unsigned char int dodge use of unsigned data type

nope. conversion int rudiment of bygone era when c language had no function prototypes. functions declared without prototypes (or left undeclared), triggered automatic promotions of smaller integer arguments int. once prototypes standard functions appeared, there intentionally tailored compatible legacy behavior. reason never see "classic" standard library function accepts [signed/unsigned] char, [signed/unsigned] short arguments (or float matter). signedness has nothing it.


Comments