Weird Program Behaviour

Is there some actual difference between these two chunks of code or is this just some weird behavior from the compilier?

When the string "'GrpmD01*-00180',$0D,$0A" is fed into the two blocks below it produces different results. Version 2 produces the expected result of -18, while version 1 produces a result of -26000something.

Version 1:
active(find_string(sTemp,'GrpmD01',1)):	//volume changes
{
	stack_var char t1[50]
		    
	t1 = sTemp
	remove_string(t1,'*',1)
		    
	nLevels = atoi(t1)/10
}

Version 2:
active(find_string(sTemp,'GrpmD01',1)):	//volume changes
{
	stack_var char t1[50]
	stack_var sinteger t2
		    
	t1 = sTemp
	remove_string(t1,'*',1)
		    
	t2 = atoi(t1)
		    
	nLevels = t2/10
}

Comments

  • Colzie
    Colzie Senior Member
    No solution, only anecdotal corroboration. I have definitely run into this as well.
  • tomk
    tomk Junior Member
    It looks like a signedness casting bug, possibly only when dividing by a number which ends in a 0 in base 10 (from what I observed).

    One reliable workaround:
    nLevels = -atoi(t1)/-10;
    

    Another is to split the operation over two lines, as you found. You can also do:
    nLevels = atoi(t1)/2;
    nLevels = nLevels/5;