Question

In "C" Language (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union integer. Each union variable should be printed as a char, a short, an int and a long. Do the values always print correctly:? Sample input/output. To print in hexadecimal, use %x as the formatter. Input must be same for all cases for comparison Enter data for type char:8 Breakdown of the element in the union char 8 short 56 int 56 long 56 Breakdown in hex char 38 short 38 int 38 long 38 Enter data for type short:8 Breakdown of the element in the union char short 8 int 8 long 8 Breakdown in hex char 8 short 8 int 8 long 8 Enter data for type int:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you


#include
union integer
{
char c;
short s;
int i;
long b;
};


void display(union integer u)
{
printf("\nBreakdown of the element in the union\n");
printf("char %c\n",u.c);
printf("short %hd \n", u.s);
printf("int %d \n", u.i);
printf("long %ld \n", u.b);
  
printf("\n\nBreakdown in hex\n");
printf("char %x\n",u.c);
printf("short %x \n", u.s);
printf("int %x \n", u.i);
printf("long %lx \n\n", u.b);

}
int main()
{
union integer u1 = {0}, u2 = {0}, u3 = {0}, u4 = {0};
  
printf("Enter data for type char: ");
scanf("%c", &u1.c);
display(u1);
  
printf("Enter data for type short: ");
scanf("%hd", &u2.s);
display(u2);
  
printf("Enter data for type int: ");
scanf("%d", &u3.i);
display(u3);
  
printf("Enter data for type long: ");
scanf("%ld", &u4.b);
display(u4);
  
}


output
======
Enter data for type char: 8

Breakdown of the element in the union
char 8
short 56
int 56
long 56


Breakdown in hex
char 38
short 38
int 38
long 38

Enter data for type short: 8

Breakdown of the element in the union
char
short 8
int 8
long 8


Breakdown in hex
char 8
short 8
int 8
long 8

Enter data for type int: 8

Breakdown of the element in the union
char
short 8
int 8
long 8


Breakdown in hex
char 8
short 8
int 8
long 8

Enter data for type long: 8

Breakdown of the element in the union
char
short 8
int 8
long 8


Breakdown in hex
char 8
short 8
int 8
long 8

Add a comment
Answer #2
// Monjur Alam
// Create union integer with members char c, short s, int i and long b.

#include <stdio.h>

union integer {
    char c;
    short s;
    int i;
    long b;
};

int main(){    
    union integer test1, test2, test3, test4;
    
    printf("Enter a char: ");
    scanf("%c", &test1.c);
    fflush(stdin);
    
    printf("Enter a short: ");
    scanf("%hd", &test2.s);
    fflush(stdin);
        
    printf("Enter an integer: ");
    scanf("%d", &test3.i);
    fflush(stdin);
        
    printf("Enter a long: ");
    scanf("%ld", &test4.b);
    fflush(stdin);
    
    printf("\n\ntest1.c as a char: %c\n", test1.c);    
    printf("test1.c as a short: %hd\n", test1.c);    
    printf("test1.c as an int: %d\n", test1.c);    
    printf("test1.c as a long: %ld\n", test1.c);
    
    printf("\n\ntest2.s as a char: %c\n", test2.s);    
    printf("test2.s as a short: %hd\n", test2.s);    
    printf("test2.s as an int: %d\n", test2.s);    
    printf("test2.s as a long: %ld\n", test2.s);
    
    printf("\n\ntest3.i as a char: %c\n", test3.i);    
    printf("test3.i as a short: %hd\n", test3.i);    
    printf("test3.i as an int: %d\n", test3.i);    
    printf("test3.i as a long: %ld\n", test3.i);
    
    printf("\n\ntest4.b as a char: %c\n", test4.b);    
    printf("test4.b as a short: %hd\n", test4.b);    
    printf("test4.b as an int: %d\n", test4.b);    
    printf("test4.b as a long: %ld\n", test4.b);  
    return 0;
}


answered by: Monjur
Add a comment
Know the answer?
Add Answer to:
In "C" Language (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program th...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) Problem 6A (5 points 10.8 (Using Unions) Create union integer with members...

    WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) Problem 6A (5 points 10.8 (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union integer. Each union variable should be printed as a char, a short, an int and a long. Do the values always print correctly? Sample input/output. To print...

  • Programing C Create union integer with members char c, short s, int i and long l....

    Programing C Create union integer with members char c, short s, int i and long l. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union integer. Each union variable should be printed as a char, a short, an int and a long. Make sure that the correct values are printed as shown below in the example An example interaction: Enter a char: A The char is:...

  • (Using Unions) Create union integer with members char c, short s, int i and long b

    This is from chapter 10 ex 10.8 in c how to program 6th ed deitelWrite a program that inputs the values of type char, short, int, and long and stores the values in union variables of type union integer. Each union variable should beprinted as a char, a short, an int and a long. D the values always print correctly?. Also create a flow chart or psuedocode of the program .

  • WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) 1o.9 (Using Unions) Create union floatingPoint with members float f, doubl...

    WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) 1o.9 (Using Unions) Create union floatingPoint with members float f, double d and long double x. Write a program that inputs values of rype float, double and long double and stores the float, a double and a long double. Do the values always print correctly? Note: The long double result will vary depending on the system (Windows, Linux, MAC) you use. So values in union variables of type union floatingPoint. Each...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr);...

    C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr); Here is an example of its use, in conjunction with print_decimal. Assume that the user will type a non-negative hex number as input. Also, assume that the “digits” a-f are in lower case. Let’s say the user types 1b int x; scan_hex(&x); print_decimal(x); MODIFY THIS CODE // function inputs a non-negative integer and stores it into *xptr. // The format of the input is...

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • I need little help with C language. I need to pass what I get from HexToBin(char*...

    I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) {    long int i = 0;    while (hexdec[i]) {        switch (hexdec[i]) {        case '0':            printf("0000");            break;...

  • please write this in "MARIE assembly language" #include <iostream> using namespace std; int DivideByTwo(int, int); //...

    please write this in "MARIE assembly language" #include <iostream> using namespace std; int DivideByTwo(int, int); // Data section int Data[] = { 0x0102, 0x0105, 0x0106, 0x0108, 0x011A, 0x0120, 0x0225, 0x0230, 0x0231, 0x0238, 0x0339, 0x0350, 0x0459, 0x055F, 0x066A, 0x0790, 0x08AB, 0x09AF, 0x0AB9, 0x0BBD, 0x0CC1, 0x0DCA, 0x0EFE, 0x0FFE }; int main() { int* BAddr = &Data[0]; int* EAddr = &Data[23]; int Count = 24; // the number of Data int Ffff = 0xffff; // value for "not found" int num; // input...

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
Active Questions
ADVERTISEMENT