× Home
Next Lec → ← Previous Lec

Unions

What is a union?

Declaring Union

                        

                        union union_name
                        {
                            datatype member1;
                            datatype member2;
                        };
                    
                    
                        

                        //Example Code
                        union books
                        {
                            int pages;
                            float price;
                            char title[20];
                        }b1;
                    
                    

Accessing Union members

We use ". "operator to access the members of a union.

                        

                         #include <stdio.h>
                         #include <string.h>
                        
                         union Book {
                            int pages;
                            float price;
                            char title[20];
                         };
                        
                         int main() {
                            union Book b1;
                            b1.pages = 100;
                            printf( "Pages: %d\n", b1.pages);
                            b1.price = 250.5;
                            printf( "Price : %.1f\n", b1.price);
                            strcpy( b1.title, "C Programming");
                            printf( "Title : %s\n", b1.title);
                            
                            return 0;
                         }
                        
                    

What are similarity between union and structures

What are difference between union and structures

Union cannot handle all members at once

why union?

We want our program to take very less memory (no wastage of memory) so it makes our program efficient.

                        
                            
                         #include <stdio.h>
                         #include <string.h>
                         union Student
                         {
                            int id;
                            int marks;
                            char fav_char;
                            char name[34];
                         };
                         int main()
                         {
                            union Student s1;
                            strcpy(s1.name, "Harry");
                            s1.fav_char = 'u';
                            s1.marks = 45;
                            s1.id = 1;
                            
                            //the one which is lastly given value only that variable hold the right value
                            
                            printf("The id is %d \n", s1.id);
                            printf("The marks is %d \n", s1.marks);
                            printf("The fav_char is %c \n", s1.fav_char);
                            printf("The name is %s \n", s1.name);
                            
                            return 0;
                         }
                        
                    

The id is 1
The marks is 1
The fav_char is ☺
The name is ☺