Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use your knowledge learned from class to provide an evaluation of the constructs

ID: 674307 • Letter: U

Question

Use your knowledge learned from class to provide an evaluation of the constructs (union and switch) in the following C programs:

1)

         int main() {

         union {

              int i;

             char *s;

         } x;

                 

         x.i = 10;

         printf(x.s);

         }

2)

int main () {

int index, odd, even;

scanf(“%d”, &index);

odd = 0;

even = 0;

switch (index) {

                  case 1:

                  case 3: odd += 1;

                  case 2:                

                  case 4: even += 1;

                  default:               

                           printf(“index=%d, odd=%d, even=%d ”, index, odd, even);

         }

}

Explanation / Answer

Union :

Union is a user defined structure which is given the size of variable which have maximum storage capacity.

In above case it would be integer.

Switch:

Switch case is used for making the decision based on the value of one variable.

In above case it is index variable.

if the index variable value is 1 or 3 then odd count will be incremented if index variable value is 2 or 4 then even count will be incremented else the value of index variable odd varaible and even variable will be printed.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote