C++
  Home arrow C++ arrow Page 9 - First Steps in (C) Programming, conclusion
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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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, conclusion
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 53
    2005-12-01

    Table of Contents:
  • First Steps in (C) Programming, conclusion
  • More Numeric Data Types
  • Try It Out: Character Building
  • Unsigned Integers: Using Positive Integers
  • Try It Out: Finding the Limits
  • Mathematical Functions
  • Designing a Program
  • The Solution
  • Summary

  • 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, conclusion - Summary


    (Page 9 of 9 )

    This chapter covered quite a lot of ground. By now, you know how a C program is structured, and you should be fairly comfortable with any kind of arithmetic calculation. You should also be able to choose variable types to suit the job at hand. Aside from arithmetic, you’ve added quite a bit of input and output capability to your knowledge. You should now feel at ease with inputting values into variables via scanf().You can output text and the values of character and numeric variables to the screen. You won’t remember it all the first time around, but you can always look back over this chapter if you need to. Not bad for the first two chapters, is it?

    In the next chapter, you’ll start looking at how you can control the program by making decisions depending on the values you enter. As you can probably imagine, this is key to creating interesting and professional programs.

    Table 2-9 summarizes the variable types and the format specifiers you’ve used so far. You can look back at these when you need a reminder as you continue through the book.

    Table 2-9.Variable Types and Value Ranges

     

     

    Number

     

    Type

    of Bytes

    Range of Values

    char

    1

    –128 to +127 or 0 to 255

    unsigned char

    1

    0 to 255

    int

    2 or 4

    –32,768 to +32,767 or –2,147,438,648 to +2,147,438,647

    unsigned int

    2 or 4

    0 to 65,535 or 0 to 4,294,967,295

    short

    2

    –32,768 to +32,767

    unsigned short

    2

    0 to 65,535

    long

    4

    –2,147,438,648 to +2,147,438,647

    unsigned long

    4

    0 to 4,294,967,295

    float

    4

    ±3.4E38 (6 digits)

    double

    8

    ±1.7E308 (15 digits)

    long double

    10

    ±1.2E4932 (19 digits)

    The output format specifiers shown in Table 2-10 are used to control the form of output for values of variables of various types when you’re using theprintf()function.

    Table 2-10.Output Format Specifiers

     

    Output Format Specifier

    Outputs

    %[-][width]c

    Character value

    %[-][width]d

    Signed decimal integer

    %[width]x

    Unsigned hexadecimal integer using "abcdef"

    %[width]X

    Unsigned hexadecimal integer using "ABCDEF"

    %[-][width]ld

    Long signed decimal integer

    Output Format Specifier

    Outputs

    %[-][width]u

    Unsigned decimal integer

    %[-][width][.precision]f

    Floating-point number without an exponent

    %[-][width][.precision]e

    Floating-point number with an exponent

    The square brackets enclose optional elements in a format specification and aren’t part of the specifier. The-specifies that the output is left-aligned in the field, the default being right-aligned. Thewidth specifies the field width as an integral number of characters..precisionspecifies the number of places that are to be displayed following the decimal point. Thus,%-15.6fis a specification for a left-aligned floating-point value in a field 15 characters wide with 6 digits following the decimal point.

    The input format specifiers shown in Table 2-11 are used to control how data is interpreted when it’s read from the keyboard by thescanf()function.

    Table 2-11. Input Format Specifiers

     

    Input Format Specifier

    Reads

    %c

    A single character

    %hd

    A value of type short

    %d

    A value of type int

    %ld

    A value of type long

    %f or %e

    A value of type float

    %lf or %le

    A value of type double

    You’ll see more onscanf()input specifiers in later chapters.

    Exercises

    The following exercises enable you to try out what you’ve learned in this chapter. If you get stuck, look back over the chapter for help. If you’re still stuck, you can download the solutions from the Downloads area of the Apress website (http://www.apress.com), but that really should be a last resort.

    Exercise 2-1. Write a program that prompts the user to enter a distance in inches and then outputs that distance in yards, feet, and inches.

    Exercise 2-2. Write a program that prompts for input of the length and width of a room in feet and inches, and then calculates and outputs the floor area in square yards with two decimal places after the decimal point.

    Exercise 2-3. You’re selling a product that’s available in two versions: type 1 is a standard version priced at $3.50, and type 2 is a deluxe version priced at $5.50. Write a program using only what you’ve learned up to now that prompts for the user to enter the product type and a quantity, and then calculates and outputs the price for the quantity entered.

    Exercise 2-4. Write a program that prompts for the user’s weekly pay in dollars and the hours worked to be entered through the keyboard as floating-point values, and then calculate and output the average pay per hour in the following form:

    --------------------------------------------
    Your average hourly pay rate is 7 dollars and 54 cents.
    --------------------------------------------


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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. Buy this book now.

    C++ ARTICLES

    - 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
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway