printf 不产生输入值 [英] printf not yielding input values
问题描述
我有以下代码块.最后一行用于生成用户输入的 X
、Y
和 P
值.但是它只返回
I have the following block of code. The final line is meant to yield the values for X
,Y
and P
values that are input by the user. However it only returns
(0,0,0)
而不是用户给定的值.我错过了什么?
instead of the values given by user. What am I missing?
printf("What is the robot's initial X position? (cm)\n");
scanf("%f",&X);
printf("What is the robot's initial Y position? (cm)\n");
scanf("%f",&Y);
printf("What is the robot's initial angular position? (degrees)\n");
scanf("%f",&P);
printf("The initial position is (%d, %d, %d)\n", X,Y,P);
推荐答案
假设 X
、Y
和 P
属于 类型double
或 float
(input part, scanf()
),需要使用 %f
或 %lf
格式说明符(根据需要)打印(或扫描)值.
Assuming X
, Y
and P
are of type double
or float
(input part, scanf()
), you need to use %f
or %lf
format specifier (as required) to print (or scan) the values.
- 打印
float
或double
,需要使用%f
- 扫描
float
,使用%f
,扫描double,使用%lf
.
- For printing
float
ordouble
, you need to use%f
- For scanning
float
, use%f
, for scanning double, use%lf
.
为特定格式说明符使用错误类型的参数是未定义行为.%d
需要一个 int
参数.因此,在您的情况下,将 %d
用于 float
或 double
类型的参数是 UB.
Using wrong type of argument for a particular format specifier isundefined behaviour. %d
expects an int
argument. So, in your case, using %d
for float
or double
type of argument is UB.
这篇关于printf 不产生输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!