Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
 Do you have a collection of Interview Questions and interested to share with us!! Please send that collection to along with your userid / name. ThanQ

 Categories >> Code Snippets >> Programming Code >> C Code

 C Code Interview Questions (400) C++ Code Interview Questions (121) VC++ Code Interview Questions (2) Java Code Interview Questions (57) Dot Net Code Interview Questions (113) Visual Basic Code Interview Questions (8) Programming Code AllOther Interview Questions (37)
Question
`What is the main difference between STRUCTURE and UNION?`
Question Submitted By :: Mariaalex007
I also faced this Question!!     Rank Answer Posted By

Re: What is the main difference between STRUCTURE and UNION?
# 1
All the members of the structure can be accessed at
once,where as in an union only one member can be used at a time.
Another important difference is in the size allocated to a
structure and an union.
for eg:
struct example
{
int integer;
float floating_numbers;
}
the size allocated here is sizeof(int)+sizeof(float);
where as in an union
union example
{
int integer;
float floating_numbers;
}
size allocated is the size of the highest member.
so size is=sizeof(float);

 Is This Answer Correct ? 639 Yes 45 No
Vijay Nag

Re: What is the main difference between STRUCTURE and UNION?
# 2
1) Structure: The size in bytes is the sum total of size of
all the elements in the structure, plus padding bytes.
2) Size of in bytes of the union is size of the largest
variable element in the union.

i.e In case of Union, the elements making up the
union 'overlap' in memory OR they are accessed as diffrent
name/type at diffrent places in the program.

Whereas in case of Struct, each of the elements have a
distinct identity.

 Is This Answer Correct ? 258 Yes 43 No
Ravi

Re: What is the main difference between STRUCTURE and UNION?
# 3
The difference between structure and union in c are: 1.
union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members. 2. In union, one block is used by all the member
of the union but in case of structure, each member have
their own memory space

 Is This Answer Correct ? 220 Yes 19 No
Dinesh Haridoss

Re: What is the main difference between STRUCTURE and UNION?
# 4
At the one time only one data member can be access...or can
be used.

 Is This Answer Correct ? 76 Yes 99 No
Anurag

Re: What is the main difference between STRUCTURE and UNION?
# 5
All the members of the structure can be accessed at
once,where as in an union only one member can be used at a
time.

 Is This Answer Correct ? 138 Yes 18 No
Om Prakash Mit

Re: What is the main difference between STRUCTURE and UNION?
# 6
. union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members.
2. In union, one block is used by all the member of the
union but in case of structure, each member have their own
memory space
1. Let’s say a structure containing an int, char and
float is created and a union containing int char float are
declared. struct TT{ int a; float b; char c; } Union UU{
int a; float b; char c; }
2. sizeof TT(struct) would be >9 bytes (compiler
dependent-if int,float, char are taken as 4,4,1)
3. sizeof UU(Union) would be 4 bytes as supposed from
above.If a variable in double exists in union then the size
of union and struct would be 8 bytes and cumulative size of
all variables in struct

 Is This Answer Correct ? 54 Yes 16 No
Priti

Re: What is the main difference between STRUCTURE and UNION?
# 7
1.)A union is a way of providing an alternate way of
describing the same memory area. In this way, you could
have a struct that contains a union, so that the "static",
or similar portion of the data is described first, and the
portion that changes is described by the union. The idea of
a union could be handled in a different way by having 2
different structs defined, and making a pointer to each
kind of struct. The pointer to struct "a" could be assigned
to the value of a buffer, and the pointer to struct "b"
could be assigned to the same buffer, but now a->somefield
and b->someotherfield are both located in the same buffer.
That is the idea behind a union. It gives different ways to
break down the same buffer area.

2.)The difference between structure and union in c are: 1.
union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members. 2. In union, one block is used by all the member
of the union but in case of structure, each member have
their own memory space

3.)Detailed Example:
struct foo
{
char c;
long l;
char *p;
};

union bar
{
char c;
long l;
char *p;
};

A struct foo contains all of the elements c, l, and p. Each
element is
separate and distinct.

A union bar contains only one of the elements c, l, and p
at any given
time. Each element is stored in the same memory location
(well, they all
start at the same memory location), and you can only refer
to the element
which was last stored. (ie: after "barptr->c = 2;" you
cannot reference
any of the other elements, such as "barptr->p" without
invoking undefined
behavior.)

 Is This Answer Correct ? 26 Yes 25 No
Anil Kumar Nahak

Re: What is the main difference between STRUCTURE and UNION?
# 8
in union the size allocated is the size of the highest
member whereas in structure the size allocated is the sum of
the size of all its declerations.

 Is This Answer Correct ? 40 Yes 8 No
Puneet Shukla

Re: What is the main difference between STRUCTURE and UNION?
# 9
The difference between union and structure can be as follows:
1. The way structure occupies memory for its member is different from union.
(a) Structure occupies appropriate separate memory for its members
(b) Union occupies memory for that member which needs largest chunk of bytes.
2. We can initialize any of the structure members while initializing any of the union member other than the first member may have unpredictable results.

 Is This Answer Correct ? 35 Yes 11 No
Asesh K Tripathy

Re: What is the main difference between STRUCTURE and UNION?
# 10
While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Example:

union exforsys_t {
char c;
int i;
float f;
} exforsys;

Defines three elements:

exforsys.c
exforsys.i
exforsys.f

Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other.

 Is This Answer Correct ? 28 Yes 16 No
Harshita

  Question Asked @ Answers Other C Code Interview Questions Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro 2 Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str); 1 int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } 1 Is this code legal? int *ptr; ptr = (int *) 0x400; 1 int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf(). 2 what is oop? 2 What is the hidden bug with the following statement? assert(val++ != 0); 1 how to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 using any loop(for or while) only once(only 1 loop) and maximum 2 variables using C. Cap-Gemini 16 void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above HCL 1 main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); } CSC 4 main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user HCL 2 pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only" 2 For more C Code Interview Questions Click Here