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

Formatted output

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



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

Formatted output
PostPosted: Wed Apr 25, 2007 6:04 am     Reply with quote

Hi!

I have to send to a serial printer a formatted output like this:

Code:

Field1   Field2   Field3
  2        2.6     12.5
  1       12.6    876.6


I can't uses spaces because I don't know the length of each number and I want the dot aligned.
What do you suggest me?

Thanks
Marco / iw2nzm
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Apr 25, 2007 6:20 am     Reply with quote

You can use the %w option of the printf formatting capabilities.
(CCS Manual P. 186)


Humberto
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Wed Apr 25, 2007 6:32 am     Reply with quote

I was unclear in my previous post.

My problem is how to align the dot of numbers without to know their length.
Also, I don't need leading zeros so I can't use fixed length format.

Thanks for your quick answer!
Marco / iw2nzm
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Apr 25, 2007 7:39 am     Reply with quote

If you canĀ“t use spaces, nor leading zeros nor fixed length, you need to write a
dedicated function equivalent to gotoxy(x,y) but with only 1 parameter.


Humberto
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Wed Apr 25, 2007 7:51 am     Reply with quote

Ok, this is what I need to know.

Thanks again
Marco / iw2nzm
Ttelmah
Guest







PostPosted: Wed Apr 25, 2007 9:59 am     Reply with quote

You can use a fixed length format.
This is the difference between (on a float), '%5.1f', and '%05.1f'. The former generates a 5 digit wide field, _without_ leading spaces, while the latter adds the leading spaces.

The former format, is commonly used to generate exactly the sort of output bing described.

Best Wishes
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Wed Apr 25, 2007 11:47 am     Reply with quote

I used the '0' option in the printf format with integers but in that case it adds leading zeros instead spaces.

I don't know this behavior with float, thank you for the tip!

Marco / iw2nzm
Ttelmah
Guest







PostPosted: Thu Apr 26, 2007 3:24 am     Reply with quote

You do understand the three possibilities?.

%d

This will print a field as wide as needed. Changing output length 'on the fly', according to the size of the number.

%4d

This will print four characters, with leading spaces.

%04d

This will print four characters, with leading zeros.

The key to remember though, is that the field must be wide enough. Remember that a number if 'signed', can have a - sign, so a standard 8bit signed value, must be given four spaces, to handle every situation, without the field changing width. If you specify:

%3d

Then have the number -100, four characters are needed, and the output will change width. The same applies with the '%w' format, where you have to allow space, both for the sign, and the decimal point, so, to make the field stay the same width, with every possible combination on a 16bit number, using %lw, you need to make the field 7 characters wide!. The commonest fault, is underestimating just how big the field needs to be. So (for instance), on a 'float', using:

%3.1f

Only allows numbers up to '9.9' to be handled without the field growing. If instead, you want to handle numbers up to 100.0, and allow for a -ve sign as well, you need to use %6.1f...
There are faults on some compiler versions, with the width specifiers not working properly, but on the current compiler, the original poster, can use these to give fixed output widths, and align the integers, and the decimal point as required.

Best Wishes
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Thu Apr 26, 2007 3:39 am     Reply with quote

Ttelmah wrote:
You do understand the three possibilities?.


I thank you very much for the time spent to write the detailed answer!
I understand, now!

Very Happy

Marco / iw2nzm
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