Upload
jordan-golden
View
216
Download
1
Embed Size (px)
Citation preview
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.
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
Value of variable a is: 1.250000
Note that the element is printed starting from the last space of element printed earlier.
Standard I/O Functions – printf()
Format Specifier Minimum field
%d, %ld, number of digits that form the decimal
Example:
15 – 2 spaces, 1500 – 4 spaces
-15 – 3 spaces, -1500 – 5 spaces
Standard I/O Functions – printf()
Format Specifier Minimum field
%x, %X number 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.
Standard I/O Functions – printf()
Format Specifier Minimum field
%c 1
%f number 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)
Standard I/O Functions – printf()
Format Specifier Minimum field
%s Depends on number of characters
Example: “Hello, World!” (13 spaces – Hello, World!)
Standard I/O Functions – printf()
Consider this example:
printf(“I am %5d years old\n”, 28);
I a m 2 8 y e a r s o l d
5 spaces are reserved for the decimal number
Right Alignment - spaces at right side will be used first
Standard I/O Functions – printf()
Consider this example:
printf(“I am %5d years old\n”, 28);
I a m 2 8 y e a r s o l d
5 spaces are reserved for the decimal number
Right Alignment - spaces at right side will be used first
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 = 2 5
0 x 0 = 0
1 5 x 1 5 = 2 2 5
1 0 x 1 0 = 1 0 0
Output
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);
D e c i m a l H e x
1 0 A
1 1 B
1 2 C
1 3 D
1 5 E
1 6 F
Standard I/O Functions – printf()
Consider this example:
printf(“%-8s%-5s\n”, “Name”,”Age”);
N a m e A g e
%-8s - 8 spaces are reserved for string
‘-’ sign signify left alignment
%-5s - 5 spaces reserved for string left alignment
Standard I/O Functions – printf()
What’s the output?:
printf(“%-8s5s\n”, ”Decimal”, ”Hex”);
D e c i m a l H e x
Output
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.
R M 3 8 8 . 8 9
Standard I/O Functions – printf()What’s the output
printf(“%-3s%7.3f\n”,” RM”, 388.88888);
R M 3 8 8 . 8 8 9
printf(“%-.2f\n”, 388.88888);
3 8 8 . 8 9
printf(“%4.2f\n”, 388.88888);
3 8 8 . 8 9
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 . 1 2
3 . 1 2 3
The asterisk ‘*’ is a variable width determined by variable i
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
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
x 148
-------------
8 times 435 is 3480
4 times 435 is 1740
1 times 435 is 435
-------------
Add the products to get 64380