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 gkittelson 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
Jun 5, 2001
1
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
Replies
11
Views
196
  • Locked
  • Question
Replies
4
Views
166
Replies
3
Views
97

Part and Inventory Search

Sponsor

Back
Top