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 third of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
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
Range of Values
–128 to +127 or 0 to 255
0 to 255
2 or 4
–32,768 to +32,767 or –2,147,438,648 to +2,147,438,647
2 or 4
0 to 65,535 or 0 to 4,294,967,295
–32,768 to +32,767
0 to 65,535
–2,147,438,648 to +2,147,438,647
0 to 4,294,967,295
±3.4E38 (6 digits)
±1.7E308 (15 digits)
±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
Signed decimal integer
Unsigned hexadecimal integer using "abcdef"
Unsigned hexadecimal integer using "ABCDEF"
Long signed decimal integer
Output Format Specifier
Unsigned decimal integer
Floating-point number without an exponent
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
A single character
A value of type short
A value of type int
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.
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.