Code Examples
  Home arrow Code Examples arrow Page 13 - First Steps in Programming
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
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? 
CODE EXAMPLES

First Steps in Programming
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 59
    2005-03-23

    Table of Contents:
  • First Steps in Programming
  • Variables That Store Numbers
  • Naming Variables
  • Basic Arithmetic Operations
  • Try It Out: Division and the Modulus Operator
  • Variables and Memory
  • Division Using Floating-Point Values
  • Defining Constants
  • Type Casting in Arithmetic Expressions
  • Try It Out: Arithmetic with Values of Type char
  • Try It Out: Finding the Limits
  • Designing a Program
  • 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 Programming - Summary


    (Page 13 of 13 )

    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
    Typeof BytesRange of Values
    char1–128 to +127 or 0 to 255
    unsigned char10 to 255
    int2 or 4–32,768 to +32,767 or –2,147,438,648 to +2,147,438,647
    unsigned int2 or 40 to 65,535 or 0 to 4,294,967,295
    short2–32,768 to +32,767
    unsigned short20 to 65,535
    long4–2,147,438,648 to +2,147,438,647
    unsigned long40 to 4,294,967,295
    float4±3.4E38 (6 digits)
    double8±1.7E308 (15 digits)
    long double10±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 the printf() function.

    Table 2-10. Output Format Specifiers

    Output Format SpecifierOutputs
    %[-][width]cCharacter value
    %[-][width]dSigned decimal integer
    %[width]xUnsigned hexadecimal integer using “abcdef”
    %[width]XUnsigned hexadecimal integer using “ABCDEF”
    %[-][width]ldLong signed decimal integer
    (Continued)
    85

    Table 2-10. Output Format Specifiers (Continued)

    Output Format SpecifierOutputs
    %[-][width]uUnsigned decimal integer
    %[-][width][.precision]fFloating-point number without an exponent
    %[-][width][.precision]eFloating-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. The width specifies the field width as an integral number of characters..precision specifies the number of places that are to be displayed following the decimal point. Thus,%-15.6f is 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 the scanf() 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 
    %for%e            A value of type float
    %lfor%le          A value of type double


    You’ll see more on scanf() 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.
    =======================================================

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


    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.

     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





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