0

Possible Duplicate:
C++: What is the printf() format spec for “float”?

I am new to C++ , and Use the classic Borland Turbo C++

with the graphics initialized , when i use printf the following way, it shows value 0. but when i use cout the correct value is displayed

float x=10;
printf("%d",x);   // displays 0
cout<<x;        // displays 10
Community
  • 1
  • 1

3 Answers3

5

You can use the %f modifier to display floating-point values with printf:

printf("%f", x);

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

Try using %f instead.

%d is for integers. Consider reading this:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Andrew De Forest
  • 6,829
  • 14
  • 39
  • 50
  • As an FYI, I would suggest not using cplusplus.com as a reference. They've historically had some inaccuracies in their API docs, though it's possible that they've fixed things up by now. – templatetypedef Jun 11 '12 at 21:18
0

If you want to print two decimals, you should use:

Printf("%2f",var);
TRR
  • 1,637
  • 2
  • 26
  • 43
jordi
  • 1
  • 1
  • 1