What is difference between structure and union?



What is difference between structure and union?..

Answer / Vandana Srivsatava

In C, both structures and unions are user-defined data types that allow you to group multiple variables together. However, they differ in their behavior when dealing with memory.nnA structure stores each member at a separate location in memory, while a union shares the same memory location for all members.nnFor example:n```cnstruct S {n int i;n char c;n};nnunion U {n int i;n char c;n};nn S s = {1, 'a'};nU u; u.i = 1;nu.c = 'a';ns.i = u.i;ns.c = u.c;nprintf("s.i = %d, s.c = %c
", s.i, s.c); // Output: s.i = 1, s.c = anprintf("u.i = %d, u.c = %c
", u.i, u.c); // Output: u.i = 1, u.c = 1 (depending on the system)

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

f(*p) { p=(char *)malloc(6); p="hello"; return; } main() { char *p="bye"; f(p); printf("%s",p); } what is the o/p?

7 Answers   Hughes,


what wud be the output? main() { char *str[]={ "MANISH" "KUMAR" "CHOUDHARY" }; printf("\nstring1=%s",str[0]); printf("\nstring2=%s",str[1]); printf("\nstring3=%s",str[2]); a)string1=Manish string2=Kumar string3=Choudhary b)string1=Manish string2=Manish string3=Manish c)string1=Manish Kumar Choudhary string2=(null) string3=(null) d)Compiler error

9 Answers   Infosys,


#define MAX 3 main() { printf("MAX = %d ",MAX ); #undef MAX #ifdef MAX printf("Vector Institute”); #endif }

1 Answers  


What is a static variable in c?

1 Answers  


Write a c program to demonstrate character and string constants?

1 Answers  


How can I determine whether a machines byte order is big-endian or little-endian?

1 Answers  


i want to make a program in which we use input having four digits(4321) and get output its reciprocal(1234).

1 Answers  


How does memset() work in C?

2 Answers  


using for loop sum 2 number of any 4 digit number in c language

1 Answers  


write a program in c to find out the sum of digits of a number.but here is a condition that compiler sums the value from left to right....not right to left..

1 Answers  


Why is c called a structured programming language?

1 Answers  


How can I read/write structures from/to data files?

1 Answers  


Categories