Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Casting Question

Status
Not open for further replies.

Rhybec

Technical User
Joined
Jun 5, 2001
Messages
1
Location
US
I am having a problem with converting a int to at floating point number. It is my understanding that the converstion should be automatic, but an explicit cast does not help either. This example resultis in Y=0 being displayed. Any explanation would be most appreciated.

#include <stdio.h>
main() {
int x;
double y=1;

x=5;
y=x; //OR y=(double)x;
printf(&quot;Y=%d\n&quot;,y);
}
 
The type-casting is working fine. It is the printf statement that is wrong. %d means to print an integer. If you want to print the double, change it to %f. That should make it work A-OK.

-Skatanic
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question Question
Replies
11
Views
722
  • Locked
  • Question Question
Replies
4
Views
476
Replies
2
Views
406
  • Locked
  • Question Question
Replies
3
Views
306

Part and Inventory Search

Sponsor

Back
Top