www.digitalmars.com         C & C++   DMDScript  

c++.command-line - Integral promotion problem

reply tjulian <tjulian removethis.oldi.com> writes:
The following program does not run as expected, since it performs a 
signed comparison (jge in asm) instead of an unsigned comparison (jae in 
asm) when comparing unsigned shorts. Is this correct or is it a bug?

I do not know how the integral promotion rules are applied in this case. 
But if I cast the (k-l) to (unsigned short)(k-l) it works as desired.

// Compile with
// dmc -3 -mn -o+all -cod test.cpp
// or
// dmc -3 -mx -o+all -cod test.cpp x32.lib
//
// run as: test x x

#include <stdio.h>

int main(int argc, char argv[])
{
    unsigned short j,k,l;
    
    j = argc;
    k = j+1;
    l = k+5;

    if (j > (k-l))
    {
        printf("j greater\n");
    }
    else
    {
        printf("j not greater\n");
    }
    
    return(0);
}

Tim
Feb 18 2003
parent "Walter" <walter digitalmars.com> writes:
That's how C works with the integral promotion rules. The unsigned shorts
get promoted to ints, which are signed.

"tjulian" <tjulian removethis.oldi.com> wrote in message
news:MPG.18bc1125c83c2330989681 news.digitalmars.com...
 The following program does not run as expected, since it performs a
 signed comparison (jge in asm) instead of an unsigned comparison (jae in
 asm) when comparing unsigned shorts. Is this correct or is it a bug?

 I do not know how the integral promotion rules are applied in this case.
 But if I cast the (k-l) to (unsigned short)(k-l) it works as desired.

 // Compile with
 // dmc -3 -mn -o+all -cod test.cpp
 // or
 // dmc -3 -mx -o+all -cod test.cpp x32.lib
 //
 // run as: test x x

 #include <stdio.h>

 int main(int argc, char argv[])
 {
     unsigned short j,k,l;

     j = argc;
     k = j+1;
     l = k+5;

     if (j > (k-l))
     {
         printf("j greater\n");
     }
     else
     {
         printf("j not greater\n");
     }

     return(0);
 }

 Tim
Feb 18 2003