This question already has an answer here:
这个问题已经有了答案:
- printf(“%f”, aa) when aa is of type int [duplicate] 2 answers
- 当aa类型为int [duplicate] 2时,打印f(“%f”,aa)
Every time I run this program I get different and weird results. Why is that?
每次我运行这个程序,都会得到不同而奇怪的结果。这是为什么呢?
#include <stdio.h>
int main(void) {
int a = 5, b = 2;
printf("%.2f", a/b);
return 0;
}
- Live Demo
- 现场演示
4 个解决方案
#1
12
printf("%.2f", a/b);
The output of the division is again of type int
and not float
.
除法的输出也是int类型,而不是浮点类型。
- You are using wrong format specifier which will lead to undefined behavior.
- 您正在使用错误的格式说明符,这将导致未定义的行为。
- You need to have variables of type float to perform the operation you are doing.
- 您需要具有float类型的变量来执行正在执行的操作。
The right format specifier to print out int
is %d
打印int的正确格式说明符是%d
#2
7
In your code, a
and b
are of type int
, so the division is essecntially an integer division, the result being an int
.
在您的代码中,a和b是int类型的,所以除法通常是整数除法,结果是整数除法。
You cannot use a wrong format specifier anytime. %f
requires the corresponding argument to be of type double
. You need to use %d
for int
type.
任何时候都不能使用错误的格式说明符。%f要求对应的参数类型为double。您需要对int类型使用%d。
FWIW, using wrong format specifier invokes undefined behaviour.
使用错误的格式说明符调用未定义的行为。
From C11
standard, chapter §7.21.6.1, fprintf()
从C11标准,章§7.21.6.1,流()
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
如果任何参数不是对应转换规范的正确类型,则行为是未定义的。
If you want a floating point division, you need to do so explicitly by either
如果您想要浮点除法,您需要通过任何一种方法显式地这样做
-
promoting one of the variable before the division to enforce floating point division, result of which will be of floating point type.
在除法之前提升一个变量来执行浮点除法,其结果是浮点类型的。
printf("%.2f", (float)a/b);
- use
float
type fora
andb
. - a和b使用浮动类型。
#3
3
You need to change the type as float or double.
您需要将类型改为float或double。
Something like this:
是这样的:
printf("%.2f", (float)a/b);
IDEONE演示
%f
format specifier is for float
. Using the wrong format specifier will lead you to undefined behavior. The division of int by an int will give you an int.
%f格式说明符用于浮点数。使用错误的格式说明符会导致未定义的行为。整数除以整数会得到整数。
#4
0
Use this instead of your printf()
使用这个代替printf()
printf("%.2lf",(double)a/b);
#1
12
printf("%.2f", a/b);
The output of the division is again of type int
and not float
.
除法的输出也是int类型,而不是浮点类型。
- You are using wrong format specifier which will lead to undefined behavior.
- 您正在使用错误的格式说明符,这将导致未定义的行为。
- You need to have variables of type float to perform the operation you are doing.
- 您需要具有float类型的变量来执行正在执行的操作。
The right format specifier to print out int
is %d
打印int的正确格式说明符是%d
#2
7
In your code, a
and b
are of type int
, so the division is essecntially an integer division, the result being an int
.
在您的代码中,a和b是int类型的,所以除法通常是整数除法,结果是整数除法。
You cannot use a wrong format specifier anytime. %f
requires the corresponding argument to be of type double
. You need to use %d
for int
type.
任何时候都不能使用错误的格式说明符。%f要求对应的参数类型为double。您需要对int类型使用%d。
FWIW, using wrong format specifier invokes undefined behaviour.
使用错误的格式说明符调用未定义的行为。
From C11
standard, chapter §7.21.6.1, fprintf()
从C11标准,章§7.21.6.1,流()
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
如果任何参数不是对应转换规范的正确类型,则行为是未定义的。
If you want a floating point division, you need to do so explicitly by either
如果您想要浮点除法,您需要通过任何一种方法显式地这样做
-
promoting one of the variable before the division to enforce floating point division, result of which will be of floating point type.
在除法之前提升一个变量来执行浮点除法,其结果是浮点类型的。
printf("%.2f", (float)a/b);
- use
float
type fora
andb
. - a和b使用浮动类型。
#3
3
You need to change the type as float or double.
您需要将类型改为float或double。
Something like this:
是这样的:
printf("%.2f", (float)a/b);
IDEONE演示
%f
format specifier is for float
. Using the wrong format specifier will lead you to undefined behavior. The division of int by an int will give you an int.
%f格式说明符用于浮点数。使用错误的格式说明符会导致未定义的行为。整数除以整数会得到整数。
#4
0
Use this instead of your printf()
使用这个代替printf()
printf("%.2lf",(double)a/b);