www.digitalmars.com         C & C++   DMDScript  

D - ? Operator Bug

I think I found a bug in the conditional ? operator.  (Or maybe it's 
just that this is my first time to use this operator.)  In the following 
example, D truncates the first "one" into "o".  The other 7 cases work 
as I expected.  I think what happens is if the two results from the 
condition are different lengths, the length of the "else" result is used 
for both.  If the "then" result is shorter, the end of it is chopped off.

Also, does anyone know why \n doesn't always give me a carriage return 
(such as between the fourth and fifth line)?

Justin


int main()
{
     int i = 1;
     printf("i=1:" \n\0);
     printf(cast(char[]) ((i == 1) ? "one" : "2" ) ~ "      " ~ \n\0 );
     printf(((i == 1) ? cast(char[]) "one" : cast(char[]) "2" ) ~ " 
  " ~ \n\0 );
     printf(cast(char[]) ((i == 1) ? "1" : "two" ) ~ "      " ~ \n\0);
     printf(\n\0);   /* The "\n" doesn't seem to work above. */
     printf(((i == 1) ? cast(char[]) "1" : cast(char[]) "two" ) ~ " 
  " ~ \n\n\0 );

     i = 2;
     printf("i=2:" \n\0);
     printf(cast(char[]) ((i == 1) ? "one" : "2" ) ~ "      " ~ \n\0 );
     printf(((i == 1) ? cast(char[]) "one" : cast(char[]) "2" ) ~ " 
  " ~ \n\0 );
     printf(cast(char[]) ((i == 1) ? "1" : "two" ) ~ "      " ~ \n\0 );
     printf(((i == 1) ? cast(char[]) "1" : cast(char[]) "two" ) ~ " 
  " ~ \n\n\0 );

     return 0;
}
Feb 20 2003