50 likes | 52 Views
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;
E N D
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 printf(“|%d|%5d|%-5d|%5.3d|\n”, i, i, i, i); printf(“|10.3f|%10.3e|%-10g|\n”, x, x, x); return 0; } Output |40| 40|40 | 040| | 839.210| 8.392e+02|839.21 |
#include <stdio.h> main() { int i; float x; i = 40; x = 839.21 printf(“|%d|%5d|%-5d|%5.3d|\n”, i, i, i, i); printf(“|10.3f|%10.3e|%-10g|\n”, x, x, x); return 0; } Output |40| 40|40 | 040| | 839.210| 8.392e+02|839.21 | %d – displays i in decimal form, using a minimum amount of space %5d – displays i in decimal form, using a minimum of 5 characters. Since i requires only 2 characters, then 3 spaces were added %-5d – 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.3d – 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; i = 40; x = 839.21 printf(“|%d|%5d|%-5d|%5.3d|\n”, i, i, i, i); printf(“|10.3f|%10.3e|%-10g|\n”, x, x, x); return 0; } Output |40| 40|40 | 040| | 839.210| 8.392e+02|839.21 | %10.3f – 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.3e –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 %-10g –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(“Item\tUnit\tPurchase\n\tPrice\tDate\n”) Item Unit Purchase Price 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.0e3 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.