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

printf repairs corrupted long long arithmetic

take a look to my programm:
When counting how often a unsigned long long integer could be divided by ten, i got strange results. Then i realised, a printf function call repairs the fault.
Who can explain the strange effect?

//-------------------------------------------------------------------
// a little C Programm (CARM) just to demonstrate:
// printf function call repairs corrupted long long arithmetic..
//
//           sometimes i awake in the middle of the night
//                        sweat flooded
//                 my heart beating like a drum
//        is C just an abbreviation for Crashing airplanes?
//-------------------------------------------------------------------
#include <ADuC7026.h>
#include"stdio.h"

void RS232_init(int);
int main (void)
{
	unsigned long long int A;
	unsigned int n;

	RS232_init(9600);
	while(1)
	{
		printf("\nStart\n");

		A=100000000000000;
		n=0;
		while(A)
		{
			printf("");
			n++;
			A /= 10;
		}
		printf("%d \n",n);	//expected 15, got 15

		A=100000000000000;
		n=0;
		while(A)
		{
			n++;
			A /= 10;
		}
		printf("%d \n",n);	//expected 15, got 2

		printf("\nPress a key\n");
		scanf("%*c");
	}
}

void RS232_init(int Baudrate)
{
							// 3% error on 115200 Baud !!!
	GP1CON  = 0x011;		// Setup tx & rx pins on P1.0 and P1.1
   	COMCON0 = 0x080;		// Setting DLAB
							// Setting DIV0 and DIV1 to DL calculated
   	COMDIV0 = 32768*1275/32/Baudrate;
   	COMDIV1 = 0x000;
   	COMCON0 = 0x007;		// Clearing DLAB
}


0