This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51 long multiple mistake

Hi:
I use c8051f410,

Program Size: data=114.1 xdata=100 code=4633

I had to use the long multiple in program and find it can not be excecute properly.

The code is as below, Is this stack overflow? How can I fix it?

long x=0;
char cHour=0xff;
char cMin=0xff;
char cSec=0xff;
long gulbegintime=0; x=(ulong)3600*(ulong)cHour; printf("1 x =%lu \n",x); gulbegintime=60*cMin; printf("2 gulbegintime =%lu \n",gulbegintime); gulbegintime+=cSec; printf("3 gulbegintime =%lu \n",gulbegintime);

print result:

1 x =4294963696
2 gulbegintime =4294967236
3 gulbegintime =4294967235

Parents
  • it can not be excecute properly

    Actually, it does execute properly. The results are correct according to the language standard.
    Prior to the multiplication, (char)0xFF is converted to 0xFFFFFFFFUL. If want it to be 0xFFUL, use unsigned char instead of char.

Reply
  • it can not be excecute properly

    Actually, it does execute properly. The results are correct according to the language standard.
    Prior to the multiplication, (char)0xFF is converted to 0xFFFFFFFFUL. If want it to be 0xFFUL, use unsigned char instead of char.

Children