Home arrow C++ arrow Page 5 - First Steps in (C) Programming, continued

First Steps in (C) Programming, continued

If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the second of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 22
November 23, 2005
  1. · First Steps in (C) Programming, continued
  2. · Variables and Memory
  3. · Integer Constants
  4. · Floating-Point Variables
  5. · More on Format Specifiers
  6. · More Complex Expressions
  7. · Defining Constants
  8. · Try It Out: The Right Types of Variables

print this article

First Steps in (C) Programming, continued - More on Format Specifiers
(Page 5 of 8 )

In the last example, you got a lot of decimal places in the output that you really didnít need. You may be good with a rule and a saw, but you arenít going to be able to cut the plank with a length of 2.500000 feet rather than 2.500001 feet. You can specify the number of places that you want to see after the decimal point in the format specifier. To obtain the output to two decimal places, you would write the format specifier as%.2f. To get three decimal places, you would write%.3f.

You can change theprintf()statement in the last example so that it will produce more suitable output:

printf("A plank %.2f feet long can be cut into %.0f pieces %.2f feet long.", 
                  plank_length, piece_count, piece_length);

The first format specification corresponds to theplank_lengthvariable and will produce output with two decimal places. The second specification will produce no decimal placesóthis makes sense here because thepiece_countvalue is a whole number. The last specification is the same as the first. Thus, if you run the example with this version of the last statement, the output will be

A plank 10.00 feet long can be cut into 4 pieces 2.50 feet long.

This is much more appropriate and looks a lot better.

The field width for the output has been determined by default. Theprintf()function works out how many character positions will be required for a value, given the number of decimal places you specify. However, you may want to decide the field width yourself. This will be the case if you want to output a column of values so they line up. If you let theprintf()function work out the field width, youíre likely to get a ragged column of output. A more general form of the format specifier for floating-point values can be written like this:


The square brackets here arenít part of the specification. They enclose bits of the specification that are optional, so you can omit either thewidth or the.precision, or both. Thewidth value is an integer specifying the total number of characters in the output: the field width. Theprecisionvalue is an integer specifying the number of decimal places that are to appear after the decimal point.

You could rewriteprintf()from the last example to specify the field width as well as the number of digits you want after the decimal point, for example:

printf("A %8.2f plank foot can be cut into %5.0f pieces %6.2f feet long.",
                  plank_length, piece_count, piece_length);

I changed the text a little to get it to fit across the page here. The first value now will have a field width of 8 and two decimal places after the decimal point. The second value, which is the count of the number of pieces, will have a field width of 5 characters and no decimal places. The third value will be presented in a field width of 6 with two decimal places.

When you specify the field width, the value will be right-aligned by default. If you want the value to be left-aligned in the field, you just put a minus sign following the%. For instance, the specification%-10.4fwill output a floating-point value left-aligned in a field width of 10 characters with four digits following the decimal point.

Note that you can specify a field width and the alignment in the field with a specification for outputting an integer value. For example,%-15dspecifies an integer value will be presented left-aligned in a field width of 15 characters.

Thereís more to format specifiers than Iíve introduced here, and youíll learn more about them later. Try some variations out using the previous example. In particular, see what happens when the field width is too small for the value.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials