Question

(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 deitel

Write 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 .
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In pseudocode:

Let u be a new union with types char, short, int, and long
Prompt user for char input
Store input in u.char
Print all union values
Prompt user for short input
Store input in u.short
Print all union values
Prompt user for ing input
Store input in u.int
Print all union values
Prompt user for long input
Store input in u.long
Print all union values

In C code:

#include

union myUnion {
char c;
short s;
int i;
long l;
};

void print(myUnion u) {
printf("As a character: %c\n", u.c);
printf("As a short: %hd\n", u.s);
printf("As an int: %d\n", u.i);
printf("As a long: %ld\n\n", u.l);
}

int main() {
myUnion u;

printf("Please enter a character: ");
scanf("%c", &(u.c));
print(u);

printf("Please enter a short: ");
scanf("%hd", &(u.s));
print(u);

printf("Please enter an int: ");
scanf("%d", &(u.i));
print(u);

printf("Please enter a long: ");
scanf("%ld", &(u.l));
print(u);

return 0;
}

Add a comment
Answer #2

#include
#include

union project
{
char c;
short s;
int i;
long l;
}u;

void main()
{
clrscr();
printf("enter data for type char: ");
scanf("%c",&u.c);

printf("\nBreakdown of the element in the union\n");
printf("char %c\n",u.c);
printf("short %d\n",u.s);
printf("int %d\n",u.i);
printf("long %ld\n",u.l);

printf("\nBreakdown in hex\n");
printf("char %x\n",u.c);
printf("short %x\n",u.s);
printf("int %x\n",u.i);
printf("long %x\n",u.l);

printf("enter data for type short: ");
scanf("%d",&u.s);

printf("\nBreakdown of the element in the union\n");
printf("char %c\n",u.c);
printf("short %d\n",u.s);
printf("int %d\n",u.i);
printf("long %ld\n",u.l);

printf("\nBreakdown in hex\n");
printf("char %x\n",u.c);
printf("short %x\n",u.s);
printf("int %x\n",u.i);
printf("long %x\n",u.l);

printf("enter data for type int: ");
scanf("%d",&u.i);

printf("\nBreakdown of the element in the union\n");
printf("char %c\n",u.c);
printf("short %d\n",u.s);
printf("int %d\n",u.i);
printf("long %ld\n",u.l);

printf("\nBreakdown in hex\n");
printf("char %x\n",u.c);
printf("short %x\n",u.s);
printf("int %x\n",u.i);
printf("long %x\n",u.l);


printf("enter data for type long: ");
scanf("%ld",&u.l);

printf("\nBreakdown of the element in the union\n");
printf("char %c\n",u.c);
printf("short %d\n",u.s);
printf("int %d\n",u.i);
printf("long %ld\n",u.l);

printf("\nBreakdown in hex\n");
printf("char %x\n",u.c);
printf("short %x\n",u.s);
printf("int %x\n",u.i);
printf("long %x\n",u.l);


getch();
}

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
Know the answer?
Add Answer to:
(Using Unions) Create union integer with members char c, short s, int i and long b
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
  • In "C" Language (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program th...

    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...

  • 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:...

  • 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...

  • 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...

  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

  • lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv...

    lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv[]) { char input[SIZE2]; char name[SIZE]; .... char resu[SIZE2], resu2[SIZE2], resu3[SIZE2]; printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); while (...) { /* use fgets to read again */ printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); } return 0; }3.1 Specification Develop an ANSI-C program that reads user information from the standard inputs, and outputs...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

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
ADVERTISEMENT