Forum Discussion
Altera_Forum
Honored Contributor
16 years agothe use of static, get wrong result
Who can tell me why I get the wrong result?
function: alt_u64 tpm_get_ticks(void) { static alt_u64 old_t=0 ; alt_u64 new_t=0; alt_u64 res_t=0; new_t = alt_timestamp(); printf("\n 1 new_t=%x, old_t=%x \n",new_t,old_t); old_t = new_t; printf("\n 2 new_t=%x, old_t=%x \n",new_t,old_t); res_t = (old_t > 0) ? new_t - old_t : 0; printf("\n 3 new_t=%x, old_t=%x \n",new_t,old_t); return res_t; } result: 1 new_t=f5f86c4e, old_t=a 2 new_t=f5f86c4e, old_t=a 3 new_t=f5f86c4e, old_t=a4 Replies
- Altera_Forum
Honored Contributor
printf is expecting 32-bit integers. I guess that what is displayed is first the low 32 bits of new_t, and then the high 32 bits.
- Altera_Forum
Honored Contributor
gcc would normally give you a warning for the printf format mismatch.
Most warnings are really errors! - Altera_Forum
Honored Contributor
Have you tried to specify printf format as %lx instead of %x ?
Besides, I know that the format %lld is also defined for 64bit values but I've never used it. - Altera_Forum
Honored Contributor
You need %llx to match a 64bit value on the Nios (and most 32bit systems).
For portability use int64_t and PRId64 from inttypes.h.