Union in C PDF
Union in C PDF
union car
{
char name[50];
int price;
};
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, *car3;
return 0;
}
union car
{
char name[50];
int price;
} car1, car2, *car3;
In both cases, union variables car1, car2, and a union pointer car3 of union
car type are created.
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
Output
Salary = 0.0
Number of workers = 100
typedef in C
typedef is a keyword used in C language to assign alternative names to
existing datatypes. Its mostly used with user defined datatypes, when names
of the datatypes become slightly complicated to use in programs.
Syntax:
In the above statements, we have declared the unit variable of type unsigned
int by using a typedef keyword.
Now, we can create the variables of type unsigned int by writing the following
statement:
1. unit a, b;
instead of writing:
1. unsigned int a, b;
struct student
{
char name[20];
int age;
};
struct student s1;
struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;
bit field is a data structure that allows the programmer to allocate memory to
structures and unions in bits in order to utilize computer memory in an efficient
manner.
Since structures and unions are user-defined data types in C, the user has an
idea of how much memory will they occupy. Accordingly, by the
implementation of bit fields, memory management becomes easy and
efficient.
Working of bitfields:
In order to understand how bit fields work, let us consider a problem, in which
we are expected to define a structured time to display the time according to
24-hour clock entered by the user with unsigned int hours, minutes and
seconds.
#include <stdio.h>
struct time
{
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
};
int main()
{
struct time t = {11, 30, 10}; // Here t is an object of the structure time
printf("Welcome to DataFlair tutorials!\n\n");
printf("The time is %d : %d : %d\n", t.hours, t.minutes, t.seconds);
printf("The size of time is %ld bytes.\n", sizeof(struct time));
return 0;
}
Output:
/tmp/DkJfzCevha.o
The time is 11 : 30 : 10
#include <stdio.h>
struct time
{
unsigned int hours: 5; // Size restricted to 5 bits
unsigned int minutes:6; // Size restricted to 6 bits
unsigned int seconds:6; // Size restricted to 6 bits
};
int main()
{
struct time t = {11, 30, 10}; // Here t is an object of the structure time
printf("Welcome to DataFlair tutorials!\n\n");
printf("The time is %d : %d : %d\n", t.hours, t.minutes, t.seconds);
printf("The size of time is %ld bytes.\n", sizeof(struct time));
return 0;
}
Output:
/tmp/DkJfzCevha.o
Welcome to DataFlair tutorials!
The time is 11 : 30 : 10
The size of time is 4 bytes.