Home C++ Page 9 - First Steps in (C) Programming, conclusion

# First Steps in (C) Programming, conclusion

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).

Author Info:
Rating:  / 63
December 01, 2005

SEARCH DEVARTICLES

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.