0

why it not prints expected output?

hello guys I am learning C (beginner). please help me with this problem why it not printing 3.14? #include <stdio.h> #define print printf int main(int argc, char *argv[]) { print("%8.2f",3.14567); } outout : 3.15 and what is the meaning of 8 (wide means what? )

13th Jan 2021, 1:38 AM
Ratnapal Shende
Ratnapal Shende - avatar
7 Respuestas
+ 4
Jan Markus in this case width will be illustrated with leading white spaces. If it would have been "%08.2" then width would be represented by leading zeros. https://code.sololearn.com/cO3KHBy9fQeJ/?ref=app
13th Jan 2021, 2:03 AM
Arsenic
Arsenic - avatar
+ 3
"%8.2f" means print a floating point number which is at leas 8 wide and have a precision of 2 Means 3.1456 will be made 3.14 { precision of 2 } And to maintain the width of 8, required spaces would be added in front {4 in this case as 3.14 is already 4 wide}
13th Jan 2021, 2:00 AM
Arsenic
Arsenic - avatar
+ 3
The right answer is 3.14567near to 3.15 so if u need 3.14 take less than 3.14500 than you will get your answer....
13th Jan 2021, 3:15 PM
aditya nayak
aditya nayak - avatar
+ 1
yes
13th Jan 2021, 5:09 PM
RD:programmer
RD:programmer - avatar
0
I understood what is "8" in my case but still unable to understand why it is printing 3.15 instead of 3.14 in above stack thread they said they will round nearest even number but here is it rounding odd?? got confused 🙁
13th Jan 2021, 3:21 AM
Ratnapal Shende
Ratnapal Shende - avatar
0
answer:because of approximation method in maths .......... here in my code bit you get the answer with example ...read comments in the code ..... https://code.sololearn.com/cXBhLYI9M5r0/?ref=app
13th Jan 2021, 10:03 AM
RD:programmer
RD:programmer - avatar
0
Jan Markus sir printf("%.2f",3.14500) ; here it also printing 3.15 😭 how ?
13th Jan 2021, 12:46 PM
Ratnapal Shende
Ratnapal Shende - avatar