Challenge K & R Chapters 2

I am a student going through a book of kerningham and richie for c. The line in the book says that -1l less than 1u , because in this case unsigned int advances to the signed long . But -1l > 1ul , because in this case -1l rises to unsigned long .

I cannot correctly understand the progress. What will be -1l value when it is increased to unsigned long? It will be great if anyone can help. Thanks.

+4
source share
4 answers

When you learn C, if you have a question, just write yourself a simple program:

 #include <stdio.h> main() { int si = -1; unsigned int ui = 1; if ( si > ui ) printf("-1l > 1u\n"); else printf("-1l <= 1u\n"); } 

You will see that -1l > 1u displayed for the output.

Since both si and ui have the same rank (they are both int s), the rule says that a negative value will increase to unsigned at a value of UINT_MAX , which is the maximum possible unsigned value.

0
source

At -1l > 1ul -1l progresses to unsigned long , and by definition and standard -1, casting to unsigned will be the large value represented by this unsigned type.

I got my inspiration from the memory of this answer here to a rather topical question.

And after looking at the C99 project, I linger, see, for example, 6.3.1.3 (2), which says that the maximum value represented by the type will be added or subtracted from the original value until it enters the new type. I must warn you that char , although it is an integer type, is handled in a special way: it is determined by the implementation if char signed or unsigned. But this, strictly speaking, is next to the question.

+1
source

Implicit promotions are one of the most difficult things in C. If you have a C code expression that looks like

if (-1l> 1ul)

then there are no "whole promotions." Both types have the same size but different signature. -1l will be converted to an unsigned long with a very large value. This is one of the rules in "ordinary arithmetic conversions."

+1
source

This is actually a conversion. Stocks go from types with a lower rank than an integer to an integer.

The rules for integer conversions in C are somewhat complicated. They comply with ISO C99 Β§6.3.1.8 ΒΆ1:

Otherwise, whole stocks are executed on both operands. Then the following advanced operands rules are applied to the rules:

If both operands are of the same type, then further conversion will not be necessary.

Otherwise, if both operands are of integer types or both have unsigned integer types, the operand with the type of a smaller integer conversion rank is converted to the type of the operand with a higher rank.

Otherwise, if the operand having an unsigned integer type has a rank greater than or equal to the type rank of another operand, then the operand with a signed integer type is converted to the operand type with an unsigned integer type.

Otherwise, if the type of the operand with a signed integer type can represent all values ​​of the type of the operand with an unsigned integer, then the operand with an unsigned integer is converted to the type from the operands with a signed integer type.

Otherwise, both operands are converted to an unsigned integer type corresponding to the type of the operand with a signed integer type.

I will try to explain to them:

Try converting to a larger type. If there is a conflict between signed and unsigned , if the larger one (including the case when two types have the same rank), it is unsigned , go with unsigned . Otherwise, use signed only if it can represent all values ​​of both types.

+1
source

All Articles