-->
Page 1 of 1

printf issue...

PostPosted: Mon May 04, 2020 11:35 am
by RichardS
Why does i=12;Serial.printf("d=%d\n", i / 10); print out "d=1"

and i=12; Serial.printf("f=%f d=%d\n", i / 10, i / 10); prints out "f=0.000000 d=1073421916", I understand why the %f is not working but why is the %d now affected?? I would still expect d=1 or to the point "f=0.000000 d=1"

RichardS

Re: printf issue...

PostPosted: Mon May 04, 2020 11:45 am
by RichardS
RichardS wrote:Why does i=12;Serial.printf("d=%d\n", i / 10); print out "d=1"

and i=12; Serial.printf("f=%f d=%d\n", i / 10, i / 10); prints out "f=0.000000 d=1073421916", I understand why the %f is not working but why is the %d now affected?? I would still expect d=1 or to the point "f=0.000000 d=1"

RichardS


I now suspect when the printf does its thing behind the scenes its taking the %f treating it as a double putting that in memory where it is 8 bytes long then placing the 4 byte int next to it and pointing to both.... as opposed to a 4 byte float and 4 byte int which will align to the pointers it calculated.... so somehow the int ends up shifted 4 bytes.... wonder why this never bit me before?? :-)