Chapter 3 printf to print integers and floatingpoint





- Slides: 5
Chapter 3 printf to print integers and floating-point numbers in various formats. /* Name: Professor Jodi Program Name: tprintf. c Date Written: 9/3/03 Author: Walter Savitch Purpose: Prints int and float values in various formats */ #include <stdio. h> main() { int i; float x; i = 40; x = 839. 21 Output |40| 40|40 | 040| | 839. 210| 8. 392 e+02|839. 21 printf(“|%d|%5 d|%-5 d|%5. 3 d|n”, i, i); printf(“|10. 3 f|%10. 3 e|%-10 g|n”, x, x, x); return 0; } |
#include <stdio. h> main() { int i; float x; Output |40| 40|40 | 040| | 839. 210| 8. 392 e+02|839. 21 | i = 40; x = 839. 21 printf(“|%d|%5 d|%-5 d|%5. 3 d|n”, i, i); printf(“|10. 3 f|%10. 3 e|%-10 g|n”, x, x, x); %d – displays i in decimal form, using a minimum amount of space return 0; } %5 d – displays i in decimal form, using a minimum of 5 characters. Since i requires only 2 characters, then 3 spaces were added %-5 d – displays i in decimal form, using a minimum of 5 characters. The value of i does not require 5 characters, spaces are added (left-justified in a field length 5) %5. 3 d – displays i in decimal form using a minimum of 5 characters overall and a minimum of 3 digits. i is only 2 digits long, an extra zero was added to guarantee 3 digits. The resulting number is only 3 characters long so 2 spaces were added for a total of 5 characters (i is right-justified)
#include <stdio. h> main() { int i; float x; Output |40| 40|40 | 040| | 839. 210| 8. 392 e+02|839. 21 | i = 40; x = 839. 21 printf(“|%d|%5 d|%-5 d|%5. 3 d|n”, i, i); printf(“|10. 3 f|%10. 3 e|%-10 g|n”, x, x, x); return 0; %10. 3 f – displays x in fixed decimal form, using 10 characters overall, with 3 digits after the decimal point. Since x requires only 7 characters } (3 before the decimal point, 3 after the decimal point, and 1 for the decimal point itself), 3 spaces precedes the x %10. 3 e –displays x in exponential form using 10 characters overall with 3 digits after the decimal point. x requires 9 characters altogether (including exponent), 1 space precedes x %-10 g –displays x in either fixed decimal form or exponential form, using 10 characters overall. printf chose to display x in fixed decimal form. The minus sign forces left justification so x is followed by 4 spaces
Escape Sequences When escape sequences appear in printf they represent actions to perform. n – new line a – alert (bell) b – backspace t – horizontal tab example printf(“Itemt. Unitt. Purchasent. Pricet. Daten”) Item Unit Price Purchase Date
printf - prints output in a spccified format scanf - reads input according to a particular format int i, j; float x, y; scanf(“%d%d%f%f”, &i, &j, &x, &y); Suppose the user enters the following input line: 1 -20. 3 -4. 0 e 3 scanf will read the line, converting its characters to the numbers they represent and then assign 1, -20, 0. 3, and -4000. 0 to i, j, x and y respectively As scanf searches for the beginning of a number, it ignores white-space characters (the space, horizontal and vertical tab, form-feed and new-line characters). As a result numbers can be put on a single line or spread out over several lines.