Hello friends how are you, today in this blog i will teach you what is union, syntax of union, how union works and many programs of union in a very simple way.
Let's start
1.It is a collection of data of different data type.
2.It is a user defined data type.
3.Data can be of int, char, float, double etc. data type.
4.We can access the member of union by making the variable of union.
5.union keyword is used to create a union.
6.The difference between structure and union is that structure can store multiple value simultaneously but union can store only one value at a time.
7.The region is that in structure each member of structure has its own memory space but in union a single memory space is shared by all members of union.
👉Syntax
union student { char name[200]; int rollno; float marks; };
2.union is a keyword.
union student { char name[200]; int rollno; float marks; }; int main() { union student student1; return 0; }
union student { char name[200]; int rollno; float marks; }student1; int main() { return 0; }
#include<stdio.h> #include<string.h> union student { char name[200]; int rollno; float marks; }; int main() { union student student1;//creating union variable strcpy(student1.name,"Lucky"); student1.rollno=201; student1.marks=85.9; printf("Student Name =%s\n",student1.name); printf("Student Rollno=%d\n",student1.rollno); printf("Student Marks=%f\n",student1.marks); } /* ### Output ### Student Name=garbage value Student Rollno=garbage value Student Marks=85.900002 */
0 Comments