C++
  Home arrow C++ arrow Page 5 - First Steps in (C) Programming, continued
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

First Steps in (C) Programming, continued
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-11-23

    Table of Contents:
  • First Steps in (C) Programming, continued
  • Variables and Memory
  • Integer Constants
  • Floating-Point Variables
  • More on Format Specifiers
  • More Complex Expressions
  • Defining Constants
  • Try It Out: The Right Types of Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    %[width][.precision]f

    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.

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C", published by Apress. We hope...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out at your favorite bookstore today. Buy this book now.

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT