C C++ program to differentiate between structure and union

C C++ program to differentiate between structure and union

Function क्या है इसका प्रयोग क्यो किया जाता है यह कितने प्रकार का होता है जानने के लिए देखें—Functions in C

C Language में Character Array या String किसे कहते है जानने के लिए देखें हमारा यह पोस्ट— Strings in C

C Language में Union क्या होता है और यह Structure से किस तरह भिन्न है जानने के लिए देखें—Unions in C

C program to differentiate between structure and union

#include<stdio.h>

struct s
{
    char c;
    int i;
    float f;
}sv;

union u
{
    char c;
    int i;
    float f;
}uv;

int main()
{
    printf("Size of sv = %d\n",sizeof(sv));
    printf("Size of uv = %d\n",sizeof(uv));
    printf("Addresses of sv = %u %u %u\n",&sv.c,&sv.i,&sv.f);
    printf("Addresses of uv = %u %u %u\n",&uv.c,&uv.i,&uv.f);
    return 0;
}

C++ program to differentiate between structure and union

#include<iostream>
using namespace std;

struct s
{
    char c;
    int i;
    float f;
}sv;

union u
{
    char c;
    int i;
    float f;
}uv;

int main()
{
    cout<<"Size of sv = "<<sizeof(sv)<<endl;
    cout<<"Size of uv = "<<sizeof(uv)<<endl;
    cout<<"Addresses of sv = "<<&sv.c<<" "<<&sv.i<<" "<<&sv.f<<endl;
    cout<<"Addresses of uv = "<<&uv.c<<" "<<&uv.i<<" "<<&uv.f<<endl;
    return 0;
}

Output of C C++ program to differentiate between structure and union

C C++ program to differentiate between structure and union

Other union programs in C C++

  1. Declaration and initialization of union
  2. Declaration and initialization of structure
  3. Array of structure
  4. Pointer to structure
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer