zessed - 6 months ago 35

C Question

So I'm just diving into C during my first few days of classes. My professor proposed a question/ more of a challenge for us so I thought I'd propose it here.

`float x,y,z;`

x = 0.0;

y = 1.0;

z = -2.5;

He wanted us to display these numbers in binary and hexadecimal without using

`%x`

`union U {`

float y;

unsigned z;

};

Thank you for your time in advance. I'm just looking for help/ an explanation on how to develop such conversion.

Answer

I think what you're professor is trying to do, is to use the float to store the actual floating point, and then convert it to an integer using the union. Once you figure out how to convert it into an integer (using the union), then it's just a matter of converting the unsigned integer into a binary number. To do the conversion from float to int is quite easy

```
union U {
float y;
unsigned z;
};
void function()
{
Union U uFloatToInteger;
uFloatToInteger.y = 6.7; // store the float
uFloatToInteger.z// need to convert this to a binary number
}
```