+ 2
Meaning of %8.2f , %2d etc...
3 odpowiedzi
+ 5
Format sepcifiers.
https://www.sololearn.com/learn/C/2914/
First digit says how many spaces are reserved for the number, including the dot.
The second digit tells the precision, i.e. how many digits after the dot will appear.
The letter tells the data type.
So %8.2f means 8 places reserved for a float number with 2 digits precision.
xxxx7.24
Where x are spaces in the output or also digits if the number is larger.
%2d
means 2-digit integer number
+ 3
%2d outputs a decimal (integer) number that fills at least 2 character spaces, padded with empty space. E.g.: 5 → " 5", 120 → "120" %6.2f outputs a floating point number that fills at least 6 character spaces (including the decimal separator), with exactly 2 digits after the ".", padded with empty space.
- 2
%2d means:
Suppose you are printing n= 18999 with printf("%2d",n) then it will reserve the output console with 2 character and if the number of digits of n are larger than the specifier then it will print with no spaces, now suppose you print with %6d then what will happen the number of character it will reserve i.e. 6 now number of digits in n is 5 so it will print with after 1 character on the console printing with 1 space in %8d then leaving 3 spaces and then the number n.
%6.2f means:
that it will reserve the output console to 6 characters and rounding off to 2 decimal places.
n=1.9890778;
then printf("%6.2f",n); will print 1.98
n=7687.87686;
then it will print 7687.87
if you write printf("%10.2f",n);
then it prints 7687.87 with three space in the beginning because total character to the output console is 7 while we have reserved 10 characters so to compensate there will be 3 space in the beginning.