Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...