Home

A program to explain UNIONS

C Programming Language 095

Program

#include <stdio.h>
#include <string.h>

union student
{
    char name[15];
    int rn;
};

void main()
{
    union student u;
    strcpy(u.name, "John");
    printf("Name = %s\n", u.name);

    u.rn = 123; // u.name now contains garbage value
    printf("Roll number = %d\n", u.rn);

    printf("\n");
}

Result

Name = John
Roll number = 123


Last Updated on

Next Post: Rules vs Guidelines →

Comments