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: Note that the element is printed starting from the last space of element printed earlier.
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, – 5 spaces
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.
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 – ) (9 spaces – )
Standard I/O Functions – printf() Format SpecifierMinimum field %sDepends 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); Iam28yearsold 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); Iam28yearsold 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 = 25 0 x 0 = 0 15 x 15 = x10 = 100 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); Decimal Hex 10 A 11 B 12 C 13 D 15 E 16 F
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
Standard I/O Functions – printf() What’s the output?: printf(“%-8s5s\n”, ”Decimal”, ”Hex”); Decimal Hex Output
Standard I/O Functions – printf() Consider this example: printf(“RM %8.2f\n”, ); %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
Standard I/O Functions – printf() What’s the output printf(“%-3s%7.3f\n”,” RM”, ); RM printf(“%-.2f\n”, ); printf(“%4.2f\n”, );
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, ); Output 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
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 times 435 is times 435 is times 435 is Add the products to get 64380