Reputation: 33
Trying to make a code have union structure.I am gonna scan integer and printf as int float,double,long double.Then scan float print as int,float,double,long double.And same procedure for double and long int.
Code Below:
union Data
{
int num_i;
float num_f;
double num_d;
long double num_ld;
};
int main()
{
union Data data;
printf("Int girin");
scanf("%d",&data.num_i);
printf("Int %d\n",data.num_i);
printf("Float %f\n",data.num_i);
printf("Double %Lf\n",data.num_i);
printf("Long DOuble %Ld\n",data.num_i);
printf("Float gir");
scanf("%f",&data.num_f);
printf("Int %d\n",data.num_f);
printf("Float %f\n",data.num_f);
printf("Double %Lf\n",data.num_f);
printf("Long DOuble %Ld\n",data.num_f);
printf("Double Gİr");
scanf("%lf",&data.num_d);
printf("Int %d\n",data.num_d);
printf("Float %f\n",data.num_d);
printf("Double %lf\n",data.num_d);
printf("Long DOuble %ld\n",data.num_d);
printf("Long gir ");
scanf("%ld",&data.num_ld);
printf("Int %d\n",data.num_ld);
printf("Float %f\n",data.num_ld);
printf("Double %lf\n",data.num_ld);
printf("Long DOuble %ld\n",data.num_ld);
getch();
}
Upvotes: 1
Views: 1346
Reputation: 153338
1) Match the field with the correct format specifier for both scanf()
and printf()
. This also implies not all warnings were enabled. Enable them.
2) Zero fill data
as the scanf()
of a union field may not completely set all bits in the union.
3) Added a hex dump.
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <memory.h>
union Data {
int num_i;
float num_f;
double num_d;
long double num_ld;
};
void uprintf(const union Data *data) {
printf("Int %d\n", data->num_i);
printf("Float %f\n", data->num_f);
printf("Double %lf\n", data->num_d);
printf("Long Double %Lf\n", data->num_ld); // was %Ld and %ld
unsigned char *x = (void *) data;
printf("hex ");
for (size_t i = 0; i < sizeof(*data); i++)
printf(" %02X", *x++);
printf("\n");
}
int main() {
union Data data;
memset(&data, 0, sizeof(data));
printf("Int girin");
scanf("%d", &data.num_i);
uprintf(&data);
memset(&data, 0, sizeof(data));
printf("Float gir");
scanf("%f", &data.num_f);
uprintf(&data);
memset(&data, 0, sizeof(data));
printf("Double Gİr");
scanf("%lf", &data.num_d);
uprintf(&data);
memset(&data, 0, sizeof(data));
printf("Long gir ");
scanf("%Lf", &data.num_ld); // was "%ld"
uprintf(&data);
//getch();
return 0;
}
Upvotes: 0
Reputation: 8286
I think you are wanting to demonstrate the use of the union to print different types without casting so
printf("Int girin");
scanf("%d",&data.num_i);
printf("Int %d\n",data.num_i);
printf("Float %f\n",data.num_f);
printf("Double %Lf\n",data.num_d);
printf("Long DOuble %Ld\n",data.num_ld);
Upvotes: 1
Reputation: 3069
Okay, to print a number as another type of number, you should be typecasting it first or something along those lines. For example;
int a = 10;
double b = 5.5;
If I wanted to print them, respectively, as a double
and as an int
, then I should be doing the following:
// %f looks for a double
printf( "%f\n", (double) a );
printf( "%d\n", (int) b );
// output would be:
// 10.000000
// 5
About your union
, I am not sure what you're expecting to have in your hands there, but you are overwriting the same memory location over and over again, with the each scanf
call. elements inside union
s share the same memory location, a memory location that is large enough to hold the largest element inside.
So, be careful about that. If you want to have all 4 by the end, you should consider using a struct
instead of a union
.
Upvotes: 0
Reputation: 105992
This code invokes undefined behavior. Using wrong conversion specification for a datatype invokes undefined behavior.
If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
In case of UB all bets are off.
Upvotes: 2