S
shmartonak
I have the following program. Under linux I've compiled it with gcc and in DOS
I've compiled it with TURBOC 2.01. I get different outputs when I run it.
/* start of program */
#include <stdio.h>
int main()
{
unsigned char u1=200, u2=200;
unsigned long l1 = 0;
unsigned int i = 0;
printf("u1 = %u and u2 = %u\n", u1, u2 );
printf("l1 = %lu\n", l1 );
l1 = u1 * u2;
i = u1 * u2;
printf("After multiplying the unsigned int result is:\n");
printf("i = %u\n", i );
printf("and the unsigned long result is:\n");
printf("l1 = %lu\n", l1 );
return 0;
}
/* end of program */
******** results with gcc ******************
u1 = 200 and u2 = 200
l1 = 0
After multiplying the unsigned int result is:
i = 40000
and the unsigned long result is:
l1 = 40000
********** results with turboc ****************
u1 = 200 and u2 = 200
l1 = 0
After multiplying the unsigned int result is:
i = 40000
and the unsigned long result is:
l1 = 4294941760
***************************
Now my questions:
Where are the TURBOC results coming from? It looks like that an
intermediate result is being stored as a signed integer type before
being promoted to an unsigned long. Is this permitted or is this a bug
or what?
Second: Is the gcc result correct? If I'm reading the standard correctly the product
of two unsigned chars will never overflow but will be given modulo 256. Shouldn't
my product be given as 40000(mod 256) = 64 and then promoted to unsigned long?
BTW, thanks to all who reponded to my previous question about array indices.
Steve
--
I've compiled it with TURBOC 2.01. I get different outputs when I run it.
/* start of program */
#include <stdio.h>
int main()
{
unsigned char u1=200, u2=200;
unsigned long l1 = 0;
unsigned int i = 0;
printf("u1 = %u and u2 = %u\n", u1, u2 );
printf("l1 = %lu\n", l1 );
l1 = u1 * u2;
i = u1 * u2;
printf("After multiplying the unsigned int result is:\n");
printf("i = %u\n", i );
printf("and the unsigned long result is:\n");
printf("l1 = %lu\n", l1 );
return 0;
}
/* end of program */
******** results with gcc ******************
u1 = 200 and u2 = 200
l1 = 0
After multiplying the unsigned int result is:
i = 40000
and the unsigned long result is:
l1 = 40000
********** results with turboc ****************
u1 = 200 and u2 = 200
l1 = 0
After multiplying the unsigned int result is:
i = 40000
and the unsigned long result is:
l1 = 4294941760
***************************
Now my questions:
Where are the TURBOC results coming from? It looks like that an
intermediate result is being stored as a signed integer type before
being promoted to an unsigned long. Is this permitted or is this a bug
or what?
Second: Is the gcc result correct? If I'm reading the standard correctly the product
of two unsigned chars will never overflow but will be given modulo 256. Shouldn't
my product be given as 40000(mod 256) = 64 and then promoted to unsigned long?
BTW, thanks to all who reponded to my previous question about array indices.
Steve
--