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