449,022 Members | 931 Online
Need help? Post your question and get tips & solutions from a community of 449,022 IT Pros & Developers. It's quick & easy.

# Sign preserving Vs value preserving

 P: n/a Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign preserving rule:- when signed and un signed objects meet in an expression, the result is always unsigned ex:- if a is an unsigned short whose value is 2,then the expression a - 3 evaluates to a very large un signed value rather than the signed value of -1 value preserving rule:- This method converts unsigned chars and unsigned shorts to int,assuming that the int type is larger than unsigned char and unsigned short respectively. if int is not larger the object is converted to unsigned int assuming 16-bit shorts and 32-bit ints in the previous example, a would be converted to int rather than unsigned int,so the result of the expression would be -1 note that the difference between sign-preserving and value-preserving rules only becomes manifest when an unsigned type is shorter than int. if both operands are unsigned ints, the result is unsigned, so that the expression 2u-3u always evaluates to a large unsigned value 1) how valid is the above explanation? 2)is there any implementation where the int type is smaller in size when compared to unsigned short and unsigned char? 3)Does the condition sizeof(short) <= sizeof(int) <= sizeof(long) always holds in ANSI-C ? or is it true for unsigned values ? Dec 5 '07 #1