CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is the difference between structure and union?

Posted by: TAMILSELVI on 11/23/2007

Contact TAMILSELVI  Contact TAMILSELVI
Category C Interview Questions
Rating (0.0) By 0 users
Added on 11/23/2007
Views 2116
Rate it!
Answers:

there are differences ,but i know a single point that is
struct sat
{
int i;
char j;
}bat;
printf("%d",sizeof(bat));
the output is 3; but in case of union...
union sat
{
int i;char j;}bat;
printf("%d",sizeof(bat));
the output is 2;
the reason is the union considers the datatype of high value,but structure considers all values equally.



 Posted by: satish    

Contact satish  Contact satish

Unions are user defined data types having collections of variables of different data types as structures but Union is different from structure in such a way that when structure type variable declared then all the member variable of structure are created in memory each within its own memory space.
But if union type variable is declared the size allocated is the size of the largest member variable in the Union because all member variable in the Union occupy the same memory space. so one variable can be initialized at a time. Union are mostly used when memory is concerned, as only ONE member variable is initialized at a time.



 Posted by: Arif Khan    

Contact Arif Khan  Contact Arif Khan

structure and union are mostly similar the only difference is in the memory allocation i.e.,structure allocates memory separately for each variable where as union allocates memory common to all its variables declared in it.



 Posted by: chaitanya    

Contact chaitanya  Contact chaitanya

The union is a structure.The main difference between structure and union is the size of the union is equal to the size of the largest member of the union where as size of the structure is the sum of the size of all members of the structure And one more thing is that we can use one member of the union at a time.



 Posted by: Vaibhav    

Contact Vaibhav  Contact Vaibhav

basic diff is tat in union v can use only one element at a time and the complier assigns the pgm with the largest size of the elements rather than adding sizes of all datatypes as in case of struct



 Posted by: abhilash    

Contact abhilash  Contact abhilash


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is pointer?
View Answer
Can we use functions within a structure?
View Answer
void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?
View Answer
Code for swapping of two numbers without using temporary variable using C.
View Answer
code To draw a three dimensional graph using c graphics
View Answer
How to write a C program for displaying a sentence without output command?
View Answer
What is difference between the test effort and the test procedure?
View Answer
What are the disadvantages of using Pointers.
View Answer
How can I convert a number to a string?
View Answer
How to write a program for pascal triangle?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12511/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.72