Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

c - printf not yielding input values

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)
");
scanf("%f",&X);
printf("What is the robot's initial Y position? (cm)
");
scanf("%f",&Y);
printf("What is the robot's initial angular position? (degrees)
");
scanf("%f",&P);
printf("The initial position is (%d, %d, %d)
", X,Y,P);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  • For printing float or double, you need to use %f
  • For scanning float, use %f, for scanning double, use %lf.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...