Showing posts with label Unions. Show all posts
Showing posts with label Unions. Show all posts

Thursday 4 August 2016

Union within Structure

Union within Structure



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
            int marks;
            union
            {
                        char name[20];
                        int usn;
            } u;
} s;
main()
{
            int  ch;
            printf("\n Enter marks:");
            scanf("%d", &s.marks);
            printf("\nName or USN choice:");
            printf("\n 1.Name 2.USN: ");
            scanf("%d", &ch);
            switch(ch)
            {
                        case 1:             printf("\nEnter name:");
                                                scanf("%s", s.u.name);
                                                printf("\nYour name is:%s", s.u.name);
                                                break;
                        case 2:             printf("\nEnter usn:");
                                                scanf("%d", &s.u.usn);
                                                printf("\nYour usn is:%d", s.u.usn);
                                                break;
            }
            printf("\nYour marks is:%d", s.marks);
}
Output:
Enter marks: 80
Name or USN choice:
1.Name 2.USN:            1
Enter name: AAAAA
Your name is: AAAAA

Your marks is: 80 

Structure and Union Initialization

Structure and Union Initialization


#include<stdio.h>
struct first
{
            int x, y;
};

union second
{
            int x, y;
};
main()
{
            struct first f1 = {1, 2};
            printf("\nf1.x = %d \t f1.y = %d", f1.x, f1.y);

            union second s1;
            s1.x = 3;
            printf("\ns1.x = %d\t", s1.x);
            s1.y = 4;
            printf("\ns1.y = %d", s1.y);

            union second s2 = {6, 7};
            printf("\ns2.x = %d\t s2.y = %d", s2.x, s2.y);

            union second s3;
            s3.x = 10;
            s3.y = 9;
            printf("\ns3.x = %d\t s3.y = %d", s3.x, s3.y);
}
Output:
f1.x = 1   f1.y = 2
s1.x = 3   s1.y = 4
s2.x = 6   s2.y = 6
s3.x = 9   s3.y = 9

Unions

The Size and Address of Unions

The Size and Address of Structure and Unions

#include<stdio.h>
#include<stdlib.h>

struct stag
{
            char c;
            int i;
            float f;
            double d;
};
union utag
{
            char c;
            int i;
            float f;
            double d;
};
main()
{
            struct stag s;
            printf("\n size of structure=%u", sizeof(struct stag));
            printf("\n size of structure=%u", sizeof(s));
            printf("\n address of structure=%u", &s);
            printf("\n address of structure members: %u %u %u %u", &s.c, &s.i, &s.f, &s.d);

            union utag u;
            printf("\n size of union=%u",sizeof(union utag));
            printf("\n size of union=%u",sizeof(u));
            printf("\n address of union=%u",&u);
            printf("\n address of union members: %u %u %u %u",&u.c,&u.i,&u.f,&u.d);
}

Output:
 size of structure=24
 size of structure=24
 address of structure=2686752
 address of structure members: 2686752      2686756          2686760          2686768

 size of union=8
 size of union=8
 address of union=2686744
 address of union members: 2686744            2686744          2686744          2686744