226k views
2 votes
Create union floatingPoint with members float f, double d and long double x. Write a program that inputs values of type float, double and long double and stores the values in union variables of type union floatingPoint. Each union variable should be printed as a float, a double and a long double. Do the values always print correcly? Note The long double result will vary depending on the system (Windows, Linux, MAC) you use. So do not be concern if you are not getting the correct answer. Input must be same for all cases for comparison Enter data for type float:234.567 Breakdown of the element in the union float 234.567001 double 0.00000O long double 0.G0OO00 Breaklo n 1n heX float e000000O double 436a9127 long double 22fde0 Enter data for type double :234.567 Breakdown of the element in the union float -788598326743269380.00OGOO double 234.567000 long double 0.G00000 Breakolon 1n heX float 0 double dd2f1aa0 long double 22fde0 Enter data for type long double:

1 Answer

2 votes

Answer:

Here the code is given as follows,

#include <stdio.h>

#include <stdlib.h>

union floatingPoint {

float floatNum;

double doubleNum;

long double longDoubleNum;

};

int main() {

union floatingPoint f;

printf("Enter data for type float: ");

scanf("%f", &f.floatNum);

printf("\\float %f ", f.floatNum);

printf("\\double %f ", f.doubleNum);

printf("\\long double %Lf ", f.longDoubleNum);

printf("\\\\Breakdown in Hex");

printf("\\float in hex %x ", f.floatNum);

printf("\\double in hex %x ", f.doubleNum);

printf("\\long double in hex %Lx ", f.longDoubleNum);

printf("\\\\Enter data for type double: ");

scanf("%lf", &f.doubleNum);

printf("float %f ", f.floatNum);

printf("\\double %f ", f.doubleNum);

printf("\\long double %Lf ", f.longDoubleNum);

printf("\\\\Breakdown in Hex");

printf("\\float in hex %x ", f.floatNum);

printf("\\double in hex %x ", f.doubleNum);

printf("\\long double in hex %Lx ", f.longDoubleNum);

printf("\\\\Enter data for type long double: ");

scanf("%Lf", &f.longDoubleNum);

printf("float %f ", f.floatNum);

printf("\\double %f ", f.doubleNum);

printf("\\long double %Lf ", f.longDoubleNum);

printf("\\\\Breakdown in Hex");

printf("\\float in hex %x ", f.floatNum);

printf("\\double in hex %x ", f.doubleNum);

printf("\\long double in hex %Lx ", f.longDoubleNum);

return 0;

}

User Saeed Heidarizarei
by
6.8k points