![]() |
Write C program to create a union of two different book |
Write C program to create the union of two different book
Union: A union is a special data type available in the C library that allows the storage of different data types in the same memory location.
It can define a union with many members but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
Definition: A union is a collection of different data types. union is a user-defined data type. data types can be used like int, float, char, etc. union can access the member of the union by making the variable of structure.
Union creates a use union Keywords.
Syntax of Union
union (union_name)
{
Data_Type variable 1;
Data_Type variable 2;
Data_Type variable 3;
Data_Type variable 4;
};
union (union_name)
{
Data_Type variable 1;
Data_Type variable 2;
Data_Type variable 3;
Data_Type variable 4;
};
Example Syntax of Union
union student
{
int id;
char name[100];
int marks;
int roll;
};
union student
{
int id;
char name[100];
int marks;
int roll;
};
How To Declare of Union in C?
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
return 0;
}
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
return 0;
}
A union name is optional and each member definition is a normal variable definition.
such as int id; or float marks; or any other valid variable definition.
At the end of the union's definition, before the final semicolon.
we can specify one or more union variables but it is optional.
It means a single variable is stored same memory location we can be used to store multiple types of data.
We can use any built-in or user-defined data types inside a union based on your requirement.
How To Declare size of Union
#include<stdio.h>
#include<string.h>
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
printf("Memory Size:%d",sizeof(s));
return 0;
}
*****OUTPUT*****
Memory Size:100
#include<stdio.h>
#include<string.h>
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
printf("Memory Size:%d",sizeof(s));
return 0;
}
*****OUTPUT*****
Memory Size:100
Access Union Data Member: A union
To access any data member of a union.
To access any data member of a union.
It can be used to access the member dot operator (.).
How To Acess Data Member of Union.
#include<stdio.h>
#include<string.h>
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
s.id=807714;
strcpy(s.name,"C Language");
s.marks=78.67;
s.rollno=627722;
printf("Student Id:%d\n",s.id);
printf("Student Name:%s\n",s.name);
printf("Student Marks:%f\n",s.marks);
printf("Student Roll Number:%d\n",s.rollno);
return 0;
}
*****OUTPUT*****
Student Id:garbage value
Student Name:garbage value
Student Marks:garbage value
Student Roll Number:627722
#include<stdio.h>
#include<string.h>
union student
{
int id;
char name[100];
float marks;
int roll;
};
int main()
{
union student s;
s.id=807714;
strcpy(s.name,"C Language");
s.marks=78.67;
s.rollno=627722;
printf("Student Id:%d\n",s.id);
printf("Student Name:%s\n",s.name);
printf("Student Marks:%f\n",s.marks);
printf("Student Roll Number:%d\n",s.rollno);
return 0;
}
*****OUTPUT*****
Student Id:garbage value
Student Name:garbage value
Student Marks:garbage value
Student Roll Number:627722
Write a C program to create a union of two different books and display the same details
#include<stdio.h>
#include<string.h>
struct Book
{
int book_id;
int book_price;
char book_name[100];
char book_author[100];
};
int main()
{
struct Book b1,b2;
b1.book_id=807714;
b1.book_price=450;
strcpy(b1.book_name,"C Language");
strcpy(b1.book_author,"Ravi Kumar");
b2.book_id=4547714;
b2.book_price=360;
strcpy(b2.book_name,"Python Language");
strcpy(b2.book_author,"Rohan Dass");
printf("Book1 Id:%d\n",b1.book_id);
printf("Book1 Price:%d\n",b1.book_price);
printf("Book1 Name:%s\n",b1.book_name);
printf("Book1 Auther:%s\n",b1.book_author); printf("Book2 Id:%d\n",b2.book_id);
printf("Book2 Price:%d\n",b2.book_price);
printf("Book2 Name:%s\n",b2.book_name);
printf("Book2 Auther:%s\n",b2.book_author);
return 0;
} *****OUTPUT*****
Book1 Id:1769365842
Book1 Price:1769365842
Book1 Name: Ravi Kumar
Book1 Auther Name: Ravi Kumar Book2 Id:1634234194
Book2 Price:1634234194
Book2 Name: Rohan Dass
Book2 Auther Name:Rohan Das
#include<stdio.h>
strcpy(b1.book_name,"C Language");
strcpy(b1.book_author,"Ravi Kumar");
b2.book_id=4547714;
b2.book_price=360;
strcpy(b2.book_name,"Python Language");
strcpy(b2.book_author,"Rohan Dass");
printf("Book1 Id:%d\n",b1.book_id);
printf("Book1 Price:%d\n",b1.book_price);
printf("Book1 Name:%s\n",b1.book_name);
printf("Book1 Auther:%s\n",b1.book_author);
printf("Book2 Id:%d\n",b2.book_id);
printf("Book2 Price:%d\n",b2.book_price);
printf("Book2 Name:%s\n",b2.book_name);
printf("Book2 Auther:%s\n",b2.book_author);
return 0;
}
*****OUTPUT*****
Book1 Id:1769365842
Book1 Price:1769365842
Book1 Name: Ravi Kumar
Book1 Auther Name: Ravi Kumar
Book2 Id:1634234194
Book2 Price:1634234194
Book2 Name: Rohan Dass
Book2 Auther Name:Rohan Das
How To Use Union Data Type
#include<stdio.h>
#include<string.h>
union BookData
{
int book_id;
int book_price;
char book_name[100];
char book_author[100];
};
int main()
{
union BookData bd;
bd.book_id=807714;
bd.book_price=450;
strcpy(bd.book_name,"C Programming Language");
strcpy(bd.book_author,"Icoderweb");
printf("Book Id:%d\n",bd.book_id); printf("Book Price:%d\n",bd.book_price);
printf("Book Name:%s\n",bd.book_name);
printf("Book Auther:%s\n",bd.book_author);
return 0; } *****OUTPUT*****
Book Id:1685021513
Book Price:1685021513
Book Name:Icoderweb
Book Auther Name:Icoderweb
#include<stdio.h>
printf("Book Id:%d\n",bd.book_id);
printf("Book Price:%d\n",bd.book_price);
printf("Book Name:%s\n",bd.book_name);
printf("Book Auther:%s\n",bd.book_author);
return 0;
}
*****OUTPUT*****
Book Id:1685021513
Book Price:1685021513
Book Name:Icoderweb
Book Auther Name:Icoderweb
Enumeration: Enumeration is a collection of named integer constants. Enumeration is user-defined data types.it can be an enum Keyword used to create enumeration.
Syntax of Enumeration
union (union_name)
enum enum_name{value1,value2,value3...value n
};
union (union_name)
enum enum_name{value1,value2,value3...value n
};
How To Use Enumeration in C?
#include<stdio.h>
enum Days {sunday,monday,tuesday,wednesday,thrusday,friday,saturday};
int main()
{
enum Days d;
d=Friday;
printf("Value of Friday:%d",d);
}
*****OUTPUT*****
Value of Friday:5
#include<stdio.h>
enum Days {
sunday,monday,tuesday,wednesday,thrusday,friday,saturday};
int main()
{
enum Days d;
d=Friday;
printf("Value of Friday:%d",d);
}
*****OUTPUT*****
Value of Friday:5