我有以下代码块。最后一行旨在生成用户输入的 X
、Y
和 P
值。但它只返回
(0,0,0)
而不是用户给出的值。我错过了什么?
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
(输入部分,scanf()
),您需要使用%f
或%lf
格式说明符(如需要)打印(或扫描)值。
- 要打印
float
或double
,需要使用%f
- 扫描
float
时,使用%f
,扫描double时,使用%lf
。
对特定格式说明符使用错误类型的参数是 undefined behaviour 。 %d
需要一个 int
参数。因此,在您的情况下,使用 %d
作为 float
或 double
类型的参数是 UB。
关于c - printf 不产生输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918889/