Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store an float value in int variable

Tags:

c

I want to store an float value in an integer variable and print that integer variable and i want to see the float value itself. Can it be done or not ?

like image 274
Ron Avatar asked Sep 18 '25 07:09

Ron


1 Answers

If you want to see the bit pattern of your float variable you could do this:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main(void) {
    uint8_t bitpattern[sizeof (float)];
    float f = 3.1414;
    memcpy(bitpattern, &f, sizeof (float));

    for (int i = 0; i < sizeof (float); i++)
      printf("%02x ", bitpattern[i]);
}
like image 62
Jabberwocky Avatar answered Sep 20 '25 22:09

Jabberwocky