Wednesday, April 22, 2020

Union in C

  • Flowchart :






  • Syntax :

union [union tag] {
   member definition;
   member definition;
   ...
   member definition;
} [one or more union variables]; 

  • Program :

#include <stdio.h>
#include <conio.h>
#include <string.h>
union student
{
            char name[20];
            char subject[20];
            float percentage;
};

void main()
{
    union student record1;
    union student record2;
    clrscr();
       printf("\n----------------UNION----------------");
       strcpy(record1.name, "Raju");
       strcpy(record1.subject, "Maths");
       record1.percentage = 86.50;

       printf("Union record1 values example\n");
       printf(" Name       : %s \n", record1.name);
       printf(" Subject    : %s \n", record1.subject);
       printf(" Percentage : %f \n\n", record1.percentage);

       printf("Union record2 values example\n");
       strcpy(record2.name, "Mani");
       printf(" Name       : %s \n", record2.name);

       strcpy(record2.subject, "Physics");
       printf(" Subject    : %s \n", record2.subject);

       record2.percentage = 99.50;
       printf(" Percentage : %f \n", record2.percentage);
       getch();
}




  • Output :


No comments:

Post a Comment

Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...