1

C99 standard(p 6.3.1.8) state that:

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

but when I try to compile such piece of code(it is not real application code, just example)

signed long int x = -1;
unsigned long int y = ULONG_MAX;
if (x < y)
    printf("1:%p %p \n", (void *) x, (void *) y);

signed short int xx = -1;
unsigned short int yy = USHRT_MAX;
if (xx < yy)
  printf("2:%p %p \n", (void *) (intptr_t) xx, (void *) (intptr_t) yy);

I have different behavior with variable that have different types. That is I have output as follows:

2:0xffffffff 0xffff 

I am using Linux and have 32-bit processor, for compilation I am using gcc and clang

gcc -std=c99 -o some \
    -pedantic -Wextra -Wall -pedantic-errors \
    -Wshadow -Waddress -Wformat \
    ./some_src.c

clang -std=c99 -o some \
    -pedantic -Wextra -Wall -pedantic-errors \
    -Wshadow -Waddress -Wformat \
     ./some_src.c
Oleg Tsyba
  • 19
  • 1
  • I don't see how printing the variables as pointers will help you. Better to print in hex format with `%X`. Why not just print "greater" or "smaller" to help you understand how the type conversions are done? Better still, in practice cast one of the different types. The standard defines what will happen when you do, not how you *should* compare different types. – Weather Vane Apr 03 '15 at 15:41
  • @WeatherVane: The address is never printed. The value just has a cast to `void *`. – Bill Lynch Apr 03 '15 at 15:41
  • @BillLynch just corrected the comment thanks. – Weather Vane Apr 03 '15 at 15:42
  • @WeatherVane, It is has no matter, how I print info here. There is only one question: why integers with different rank is compared by different rules, whereas the standard stated that all integer types are compared identically(in such way as I pointed in quotation)? (I'm sorry if I make grammar mistakes, since I am ukrainian. – Oleg Tsyba Apr 03 '15 at 16:39
  • As It was pointed above, such question was asked before, and had a good explanation, but can someone to point where such behavior is described in the standard. – Oleg Tsyba Apr 03 '15 at 16:46
  • I am already find the answer in the standard, it is 6.3.1.1(2), thanks a lot to all. – Oleg Tsyba Apr 03 '15 at 16:54

0 Answers0