+ 1
printf("Pi = %3.2f", 3.14159); /* Pi = 3.14 */ printf("Pi = %8.5f", 3.14159); /* Pi = 3.14159 */
Cause of using 3.2 and 8.5
1 Antwort
+ 1
They are used to format output of printed values
printf specifiers have the following format:
%[flag][width][.digits][specifier]
[flag] is used to justify output / to show positive number sign (+)....
[width] = is a minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces
[.digits] = for f specifier is number of digits after decimal point to be printed.
for
printf("Pi = %3.2f", 3.14159);
width = 3, but 4 character are printed (3.14), so output is not padding with spaces
.precision = 2, only two digits of the decimal part are printed (.14)
output: Pi = 3.14
printf("Pi = %8.5f", 3.14159);
width = 8, but 7 character are printed (3.14159), so 1 extra space is printed before 3.14159
.precision = 5, only five digits of the decimal part are printed (.14159)
output: Pi = [space]3.14159