passing multiple variable to sprintf() gives wired result

Hi, am new to Arduino. The environment Arduino created is great. Easily get people hooked up.
But I bump into some glitches that I am still trying to wrap my head around.
It would be great if someone can help me out.
The sprintf function doesn't seem to work when passing multiple variables which is what m trying to do...

int t1 = 90000;
int t0 = 90000;
char buff[100] = "" ;


sprintf( buff, "t1=%d, t2=%d", t1, t0 );



t1 += 1000;
t0 += 1000;


t0 never gives any value in buff, until t1 overflows. t1 is ok.
Maybe I use the sprintf wrong ? My C is pretty rusted but I think it is how it should be used... ?

int on an Arduino is 16bit, so max value you can store in one is 32,767, or if unsigned is 65,535. Your 90,000 is blowing the budget :wink:

Use a long.

sprintf( buff, "t1=%d, t2=%d", t1, t0 );

t1 and t2, huh? So, why is the variable called t0?

When you get the type right, you'll need to make the format specifier match the type. %d is for int. It is NOT for long. %ld is for long.

sorry the code is really just right out of my head w/o checking or anything. Like you can tell from the t1 t2 t0 mass. In the tested code, I did try %ld, %lud, uint32_t, long, long long, passing arrays, nothing worked.

Actually I am trying to avoid some decimal fraction calculation by making the numbers bigger. Now I hear your advise, I recheck everything, and it works. There turned out to be another problems:

When I use long, I am supposed to use typed constant to force calculation in long. That's why I always get 0 even if I used %ld. mixing them together, I got the impression nothing works.

I have been long trying to avoid C / C++, since the dynamic language like py, lua, js etc worked well for me, now m reading chapter long type, it feels like starting all over again from basics.
Thanks for pointing out the type problem . :slight_smile: