Download presentation
Presentation is loading. Please wait.
Published byJordan Golden Modified over 9 years ago
1
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum length, beginning on the current position on the current line.
2
Standard I/O Functions – printf() Example: printf(“%s”, “Value of variable “); printf(“%c”, ‘a’); printf(“%s”, “ is: “); printf(“%f\n”, 1.25); printf(“%s%c%s%f\n”, “Value of variable”,’a’,” is: “, 1.25); Output Value of variable a is: 1.250000 Note that the element is printed starting from the last space of element printed earlier.
3
Standard I/O Functions – printf() Format SpecifierMinimum field %d, %ld, number of digits that form the decimal Example: 15 – 2 spaces, 1500 – 4 spaces -15 – 3 spaces, -1500 – 5 spaces
4
Standard I/O Functions – printf() Format SpecifierMinimum field %x, %Xnumber of digits that form the hexadecimal Example 4 (1 space – 4) 40 (2 spaces – 28) -4 (8 spaces – fffffffc) -40 (8 spaces – ffffffd8) Note that when it is a negative number, 8 spaces are used.
5
Standard I/O Functions – printf() Format SpecifierMinimum field %c1 %fnumber of digits that form the integer part 1 space for decimal point 6 digit for fractional part Example 1.25 (8 spaces – 1.250000) 11.23 (9 spaces – 11.230000)
6
Standard I/O Functions – printf() Format SpecifierMinimum field %sDepends on number of characters Example: “Hello, World!” (13 spaces – Hello, World!)
7
Standard I/O Functions – printf() Consider this example: printf(“I am %5d years old\n”, 28); Iam28yearsold 5 spaces are reserved for the decimal number Right Alignment - spaces at right side will be used first
8
Standard I/O Functions – printf() Consider this example: printf(“I am %5d years old\n”, 28); Iam28yearsold 5 spaces are reserved for the decimal number Right Alignment - spaces at right side will be used first
9
Standard I/O Functions – printf() What’s the output?: for (i = 0; i < 20; i += 5) printf(“%2d x %2d = %3d\n”, i, i, i *i); 5 x 5 = 25 0 x 0 = 0 15 x 15 =225 10 x10 = 100 Output
10
Standard I/O Functions – printf() What’s the output?: printf(“%8s5s\n”,”Decimal”,”Hex” for (i = 10; i < 16; i ++) printf(“%8d%5X\n”, i, i); Decimal Hex 10 A 11 B 12 C 13 D 15 E 16 F
11
Standard I/O Functions – printf() Consider this example: printf(“%-8s%-5s\n”, “Name”,”Age”); Name Age %-8s - 8 spaces are reserved for string ‘-’ sign signify left alignment %-5s - 5 spaces reserved for string left alignment
12
Standard I/O Functions – printf() What’s the output?: printf(“%-8s5s\n”, ”Decimal”, ”Hex”); Decimal Hex Output
13
Standard I/O Functions – printf() Consider this example: printf(“RM %8.2f\n”, 388.88888); %8.2f - 8 spaces are reserved for the floating point number where 2 of the spaces are for the fractional/precision part. Right Alignment - spaces at right side will be used first Simply change to %-8.2f to have left alignment If the number of spaces for fractional part is not specified, (example : %8f), default (6) would be used. If the spaces specified are not enough (example: %3f), default setting would be used. RM 388.89
14
Standard I/O Functions – printf() What’s the output printf(“%-3s%7.3f\n”,” RM”, 388.88888); RM 388.889 printf(“%-.2f\n”, 388.88888); 388.89 printf(“%4.2f\n”, 388.88888); 388.89
15
Standard I/O Functions – printf() Can the field width be specified by a variable? YES! Consider this example for (i = 0; i < 4; i++) printf(“%.*f\n”, i, 3.123456); Output 3 3.1 3.12 3.123 The asterisk ‘*’ is a variable width determined by variable i
16
Standard I/O Functions – printf() What’s output for (i = 1; i < 4; i++) printf(“%-*d%*d\n”, i, i, i, i); Output 1 1 2 2 3 3
17
Standard I/O Functions – printf() Develop an interactive program that can be used to teach multiplication to grade school students. The program should prompt the user to enter two three- digit integers, verify that they are indeed three-digit integers, and then print the product of the integers according to the following format 435 x148 ------------- 8 times 435 is 3480 4 times 435 is 1740 1 times 435 is 435 ------------- Add the products to get 64380
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.