Member Avatar for onaclov2000

Ok, so this is confusing me alot, right now I have a structure as such:

struct f_c_8 {
   union {
      float f;
      unsigned char c[8];
   };
};

now I declare a variable of that type

f_c_8 Time;

However,

The following prints correct number

fprintf (output_file_pointer,"\t\tTime: %f\n",event_data->Time);

And the following doesn't print correct number

fprintf (output_file_pointer,"\t\tTime: %f\n",event_data->Time.f);

What I'm confused about is that I'm passing the fprintf the entire structure and saying print a floating point number from that, but if I actually call the floating point number variable, it doesn't print the floating point correctly.

Does this make sense to anyone, anywhere?

Seems more like C++. Do you have a minimal compileable snippet that produces the results you see?

#include <stdio.h>

struct f_c_8
{
   union
   {
      float f;
      unsigned char c[8];
   } u;
};

struct f_c_8 Time = {{123.456}};

int main()
{
   fprintf (stdout, "Time: %f\n", Time); 
   fprintf (stdout, "Time: %f\n", Time.u); 
   fprintf (stdout, "Time: %f\n", Time.u.f); 
   return 0;
}

/* my output
Time: 0.000000
Time: 0.000000
Time: 123.456001
*/
#include <stdio.h>

struct f_c_8
{
   union
   {
      float f;
      unsigned char c[8];
   };
};

f_c_8 Time = {{123.456}};

int main()
{
   fprintf (stdout, "Time: %f\n", Time); 
   fprintf (stdout, "Time: %f\n", Time.f); 
   return 0;
}

/* my output
Time: 0.000000
Time: 123.456001
*/
commented: Clean and to the point as usual :) +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.