I am creating a program for homework, and the goal is trying to teach us how a union works conceptually. I believe I understand this concept, but I am having difficulty with one of my outputs; Long Double as Hex.
I am under the assumption anything long needs a capital L after the percent sign when printing. For example to print a long double float:
printf("\nLong double: %Lf\n", value.ld);
Similarly, if I wanted to print a Long Double float in hex I would use:
printf("\nLong double: %Lx", value.ld);
My professor's output is as follows:
Professors Output long double 22fde0
My Output is as follows:
My Output long double 62fe40
The differences are in the printed value for Long Double in hex. Is this a value that might be different from computer to computer or am I making a mistake in my thinking.
My code
#include <stdio.h>
// define union data
union data {
float f;
double d;
long double ld;
}value; //union variable
// begin main function
int main(void){
// get initial user input
printf("Enter data for type float: ");
scanf(" %f", &value.f);
puts("\nBreakdown of the element in the union: ");
printf("Float: %f", value.f);
printf("\nDouble: %f", value.d);
printf("\nLong double: %Lf\n", value.ld);
puts("\nBreakdown in hex: ");
printf("Float: %x", value.f);
printf("\nDouble: %x", value.d);
printf("\nLong double: %Lx", value.ld);
// get user input
printf("\n\nEnter data for type double: ");
scanf(" %lf", &value.d);
puts("\nBreakdown of the element in the union: ");
printf("Float: %f", value.f);
printf("\nDouble: %f", value.d);
printf("\nLong double: %Lf\n", value.ld);
puts("\nBreakdown in hex: ");
printf("Float: %x", value.f);
printf("\nDouble: %x", value.d);
printf("\nLong double: %Lx", value.ld);
// get user input
printf("\n\nEnter data for type long double: ");
scanf(" %lf", &value.ld);
puts("\nBreakdown of the element in the union: ");
printf("Float: %f", value.f);
printf("\nDouble: %f", value.d);
printf("\nLong double: %Lf\n", value.ld);
puts("\nBreakdown in hex: ");
printf("Float: %x", value.f);
printf("\nDouble: %x", value.d);
printf("\nLong double: %Lx", value.ld);
return 0;
} // end main function