Printf behaviour with two unsigned longs..

Could someone please try this and post the result… for my sanity…


#class auto
#memmap xmem
void main(void)
{
	long start_time;
	long diff;

	start_time=SEC_TIMER;

	while (1)
	{
  		costate
  		{
  			waitfor(DelaySec(1));
  		   diff	= (long)(SEC_TIMER-start_time);
			printf("SEC_TIMER=%u start_time=%u
",SEC_TIMER, start_time);
			printf("start_time=%u
", start_time);
			printf("diff=%ld   inline=%ld

", diff, (long)(SEC_TIMER-start_time));
		}
	}
}

I get this output…

SEC_TIMER=21321 start_time=18
start_time=21320
diff=1 inline=1

SEC_TIMER=21322 start_time=18
start_time=21320
diff=2 inline=2

SEC_TIMER=21323 start_time=18
start_time=21320
diff=3 inline=3

SEC_TIMER=21324 start_time=18
start_time=21320
diff=4 inline=4

SO…

On the first printf line, the start_time is not working, but it is fine on the second printf on its own… WTF??? Is this a possible compiler problem??

I am using DC10.66 with a RCM5400W if it makes any difference…

Dave

Hi Dave,

You should be using %lu for the printfs, %u is unsigned 16bit value, %lu is unsigned 32 bit value. The program was printing the low order word of SEC_TIMER and then the high order word of SEC_TIMER in the first printf. It never got to start time at all.

Regards,
Peter

Hello,
I got this output

SEC_TIMER=200 start_time=0
start_time=130
diff=70 inline=70

SEC_TIMER=201 start_time=0
start_time=130
diff=71 inline=71

SEC_TIMER=202 start_time=0
start_time=130
diff=72 inline=72

SEC_TIMER=203 start_time=0
start_time=130
diff=73 inline=73

SEC_TIMER=204 start_time=0
start_time=130
diff=74 inline=74

I am using the 10.64