Use of typedef in Union

We will use typedef with the union, in the same way we use with structure.

Just we have to replace the keyword ‘struct’ with the keyword ‘union’.


typedef union student
char name[20];
int age;
int main()
printf("Details of the student are as below:\n");

st s1, s2;

printf("First student\n");
printf("Name: %s\n",;
printf("Age: %d\n",s1.age);

printf("Second student\n");
printf("Name: %s\n",;
printf("Age: %d\n",s2.age);

return 0;



The code works fine without any error, but the output doesn’t seem to be the desired one because of some limitation union.

The aim of writing the above code is just to make you understand that we can use the typedef keyword in the union too.


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.