-->
Page 1 of 2

(s)printf not working WRT '%.'

PostPosted: Mon Dec 03, 2018 6:40 am
by FreddyVictor
I had a search but couldn't see anything on this....

Code: Select allSerial.printf("Time: %2d:%.2d:%.2d", 10, 11, 5);

outputs:
Time: 10:%.2d:%.2d
instead of:
Time: 10:11:05

apologies if this has been covered B4 !

Re: (s)printf not working WRT '%.'

PostPosted: Mon Dec 03, 2018 7:20 am
by QuickFix
An integer value can't contain a precision specifier (the "." dot), try this instead:
Code: Select allSerial.printf("Time: %2d:%2d:%2d", 10, 11, 5);

Re: (s)printf not working WRT '%.'

PostPosted: Mon Dec 03, 2018 7:37 am
by FreddyVictor
QuickFix wrote:An integer value can't contain a precision specifier (the "." dot), try this instead:
Code: Select allSerial.printf("Time: %2d:%2d:%2d", 10, 11, 5);

Thanks for your quick reply ;)
Your code is using the width format specifier

Your link to the reference page shows:
.precision
.number
For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros.

This works in all other c compilers I've used
I really can't believe I'm the first to notice this
Or is this just a 'known feature' ?

Re: (s)printf not working WRT '%.'

PostPosted: Mon Dec 03, 2018 11:40 am
by QuickFix
Ah yes, ignore my reply; just like with my own language (Pascal) you should be able to use precision with integer values in C++, not sure why it doesn't in your example.