DWQA QuestionsCategory: ProgramC language basic input output function
clear and shallow asked 2 months ago

include <stdio.h>
int main(void) {

float f = 123.456;
printf("%12f",f);
return 0;

}
The output result of Microsoft Visual C + + is 123.456001. Excuse me, boss, the result in the textbook is 123.456000. Which is right?
Is the “.” in the string a character? Do these 12 digits contain the point number?

2 Answers
BaLaLaLs answered 2 months ago

Either 123.456001 or 123.456000 is correct because there is a deviation in the floating-point calculation of the computer. It’s not that you wrote the program wrong, printf (% 12F “, f); 12 refers to the field width

%[width][.precision][modifier]f

Square brackets represent optional specifications.
Width or. Precision or modifier or any combination of these can be omitted.
The width value is an integer that specifies the total number of characters that contain spaces. The precision value is an integer that specifies the number of decimal places after the decimal point.
When the output value is of type long double, the modifier part is l, otherwise it is omitted.

LogM answered 2 months ago

There are errors in floating-point variables in computer. When judging whether two floating-point variables are equal, we can’t simply use “=”