printf 不产生输入值 [英] printf not yielding input values

查看:40
本文介绍了printf 不产生输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码块.最后一行用于生成用户输入的 XYP 值.但是它只返回

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);

推荐答案

假设 XYP 属于 类型doublefloat (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.

  • 打印floatdouble,需要使用%f
  • 扫描float,使用%f,扫描double,使用%lf.
  • For printing float or double, you need to use %f
  • For scanning float, use %f, for scanning double, use %lf.

为特定格式说明符使用错误类型的参数是未定义行为.%d 需要一个 int 参数.因此,在您的情况下,将 %d 用于 floatdouble 类型的参数是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆