0

Can someone please explain me the reasons for output....for this programme

Code: #include <stdio.h> void main() { float g=123.456789; printf("\n %.1f",g); printf("\n %.2f",g); printf("\n %.3f",g); printf("\n %.4f",g); } Output: 123.5 123.46 123.457 123.4568 My doubts are in comment section

11th Sep 2019, 8:21 AM
Saheb Kumar
Saheb Kumar - avatar
4 Answers
+ 1
Why is in the output 4 missing in first line 5 missing in second line 6 missing in third line And 7 missing in fourth line...???
11th Sep 2019, 8:22 AM
Saheb Kumar
Saheb Kumar - avatar
+ 1
For me the output is: 1231 1232 1233 1234 int main() { float g=123.456789; printf("%.f1 ",g); printf("%.f2 ",g); printf("%.f3 ",g); printf("%.f4 ",g);
11th Sep 2019, 8:32 AM
KfirWe
KfirWe - avatar
+ 1
The fractal portion of the number is rounded up to fit in <x> digit allowed - as specified by format specifier "%.xf". "%.1f" (only 1 decimal point) 123.456789 => rounded up to 123.5 "%.2f" (2 decimal points) 123.456789 => rounded up to 123.46 "%.3f" (3 decimal points) 123.456789 => rounded up to 123.457 "%.4f" (4 decimal points) 123.456789 => rounded up to 123.4568 Hth, cmiiw
11th Sep 2019, 9:04 AM
Ipang