CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Printing a variable number of leading zeros?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

Printing a variable number of leading zeros?
PostPosted: Thu Feb 16, 2006 9:36 pm     Reply with quote

Hi,

I have a very simple statement:
Code:
 printf("%03lu",data);


Works great. I would like to let the user decide at run time how many leading zeros to print, say from 0 to 3. Or maybe 0 to 5. I've confirmed by experimentation that, as I suspected, printf() won't take a variable for the digit or digits in the printf() format. That would be far too easy.

Needless to say, I don't have enough program space left to do a switch/case or some other means to select one of several printf statements. Do you gurus have any suggestions, or does someone know some trick with printf() that I don't?
ak6dn



Joined: 08 Jan 2006
Posts: 23
Location: Saratoga, CA USA

View user's profile Send private message

Re: Printing a variable number of leading zeros?
PostPosted: Fri Feb 17, 2006 1:40 am     Reply with quote

dbotkin wrote:
Hi,

I have a very simple statement:
Code:
 printf("%03lu",data);


Works great. I would like to let the user decide at run time how many leading zeros to print, say from 0 to 3. Or maybe 0 to 5. I've confirmed by experimentation that, as I suspected, printf() won't take a variable for the digit or digits in the printf() format. That would be far too easy.

Needless to say, I don't have enough program space left to do a switch/case or some other means to select one of several printf statements. Do you gurus have any suggestions, or does someone know some trick with printf() that I don't?


Well, you might think this would work, but it does not:
Code:
char format[8];
sprintf(format, "%%0%dd\r\n", 6);
printf(format, 12);

It just prints the format statement ('%06d\r\n'). I believe that CCS C actually parses the printf format at compile time and constructs a custom print format routine for each instance (rather than having a full blown generic printf routine always added to your code).

The smallest routine I could construct is:
Code:
char number[9];
sprintf(number, "%08d", 12);
printf(&number[8-n]);

where 'n' is the width of the output string you want to have (eg, 1-8). You should be able to optimize usage of this construct by embedding it in a subroutine, called with the number you want to print and the field width.
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

Re: Printing a variable number of leading zeros?
PostPosted: Fri Feb 17, 2006 4:45 pm     Reply with quote

Quote:
The smallest routine I could construct is:
Code:
char number[9];
sprintf(number, "%08d", 12);
printf(&number[8-n]);

where 'n' is the width of the output string you want to have (eg, 1-8). You should be able to optimize usage of this construct by embedding it in a subroutine, called with the number you want to print and the field width.

By George, I knew someone would have a good idea. I'll have to see if I can squeeze in another printf. I actually simplified the code in my first post; it's actually an sprintf, which puts ASCII charcters into an array that is then used to send the output in a different format. I may be able to just follow the sprintf with an fprintf. If that won't fit, I can always resort to a block of #ASM or something to do what I need.

Thanks!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group