problem with sprintf in code vision

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi
I am using the STK 200 and 8515 with the Code Vision C compiler.
I needed for my project to build an alarm clock as one application
I managed to do every thing good exept for the display.

I had to use the sprintf command in order to show the counter on the display .
The display started to show the right time but as soon as the seconds had to go from 9 to 10 i got this display 00:00:010
And it should show 00:00:10.
The command that i wrote is.......sprintf(array,"%02d:%02d:%02d",hour,second, hour);

I think that there is a bug in the command
please advise

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is from Borland C++ 4.52 Help

n At least n characters are printed. If the output value has less than n characters, the output is padded with blanks (right-padded if - flag given, left-padded otherwise).

0n At least n characters are printed. If the output value has less than n characters, it is filled on the left with zeros.

Using sprintf(array,"&2d:%2d:%2d"",hour,minute,second);
should give you what you want

Using sprintf(array,"%02d:%02d:%02d",hour,minute,second);
maybe what is giving you the extra 0 in the seconds position

Isn't this suppose to be "hour,minute,second" not "hour,second, hour"?
sprintf(array,"%02d:%02d:%02d",hour,second, hour);
I'm thinking that was just a typeo when entering your question.

If the above does work, let me know...
Mike

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mike,

Why would be want to print a space in front of the minutes or seconds, if they're less than 10? Most clocks, etc, might use a space in front of the hour, but it should be " 9:04:02", not " 9: 4: 2:".

The only time the width specifier won't be honored is when the value is greater than the width specified, in which case you'll usually get some like this "100:100:100", if the hours, minutes, and seconds are all 100.

His original sprintf is the correct way to do it, with the exception of the last parameter being hour, instead of seconds.

I just ran this with the latest release I have, and it is indeed a genuine bug. I've forwarded it on to Pavel.

--John

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

Just use %02u instead of %02d.

Cheers

Pavel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all
Pavel..... you are the man

It is working.
thank you it is very nice to work with proffetionals