Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

68.User defined type :Union, Union as generic type, Enum

 

Union:

It is a user defined type looks similar to structure but differs in functionality

It is a heterogeneous collection of members defined with a name.

The definition of union doesn't allocate the memory allocation of members.

When a variable or an instance of a union is created then a common memory allocation to all the members is allocated.

The size of allocated memory is equal to the size of member which has greater length.

The common memory is used by any of its members.

Any single member can be used at a time.

Union is mostly used to define generic type.

 

Syntax:

union <name>

{

      ----------------------

      ---------------------- List of members

      ----------------------

      ----------------------

};

 

 

 

Example:

struct demo

{

    float x;

    int y;

    char ch;

};

 

struct demo var;

 

var

 
 

 

 

 

 


union demo

{

      float x;

      int y;

      char ch;

};

 

union demo var;

 

 

 

 

 

 


                                                                                                         

 

Example:

#include<stdio.h>

struct demo1

{

    float x;

    int y;

    char ch;

};

union demo2

{

    float x;

    int y;

    char ch;

};

 

void main()

{

    struct demo1 var1;

    union demo2 var2;

    clrscr();

    printf("Size of struct variable %d bytes",sizeof(var1));

    printf("\nSize of union variable %d bytes",sizeof(var2));

    getch();

}

 

Output:

Size of struct variable 7 bytes

Size of union variable 4 bytes

 

Example explained:

The structure variable has the memory allocation of all members.

Where as the union variable allocates the common memory to all the members.

 

Example1:

#include<stdio.h>

#include<math.h>

union demo

{

   int x;

   char ch[2];

};

void showbits(int);

void main()

{

   union demo a;

   clrscr();

   a.x=615;

   printf("a.x=");

   showbits(a.x);

   printf("a.ch[0]=");

   showbits(a.ch[0]);

   printf("a.ch[1]=");

   showbits(a.ch[1]);

   getch();

}

void showbits(int n)

{

   long bin;

   int i;

   for(bin=0,i=0;n!=0;n=n/2,i++)

       bin=bin+(n%2)*pow(10,i);

   printf("\n%ld",bin);

}

 

Output:

a.x=1001100111

a.ch[0]=1100111

a.ch[1]=10

 

Example explained:

The showbits() is a function accepts a number and print its binary equal.

"x" is the member with maximum length hence the union variable takes 2 bytes.

Another member ch[2] uses the same memory.

When 615 is assigned to "x" then its binary equal (1001100111) is stored in the memory.

Here ch[0] is 1100111 and ch[1] is 00000010

 

Example2:

#include<stdio.h>

union demo

{

   int x;

   char ch[2];

};

void main()

{

   union demo a;

   clrscr();

   a.x=615;

   printf("a.x=%d",a.x);

   printf("\na.ch[0]=%c",a.ch[0]);

   printf("\na.ch[1]=%c",a.ch[1]);

   getch();

}

 

Output:

a.x=615

a.ch[0]=g

a.ch[1]=☻

 

Example explained:

Equal character to 01100111 is g (103)

Equal character to 00000010 is ☻(2)

 

 

 

 

 

Union as generic type:

A union variable may be used to store any value of any type.

 

Example:

#include<stdio.h>

union demo

{

    float x;

    int y;

    char z;

};

void main()

{

   union demo a,b,c;

   clrscr();

   a.x=234.768956;

   b.y=615;

   c.z='s';

   printf("x=%f",a.x);

   printf("\ny=%d",b.y);

   printf("\nz=%c",c.z);

   getch();

}

 

Output:

x=234.768951

y=615

z=s

 

Example explained:

Here a, b and c are of same union demo type but used to store float, int and char types by using different members.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Enum:

It is a user defined type used to define a series of constants.

Values of constants are implicitly initialized from "0".

Sequence of constants can be changed by explicitly assigning a value to the constant.

This type is used to improve the readability of an application.

 

Syntax:

enum <name>{ list of constants };

 

Example1:

 

#include<stdio.h>

enum days{sun,mon,tue,wed=10,thu,fri,sat};

void main()

{

  enum days x=sat;

  clrscr();

  printf("sun=%d",sun);

  printf("\nfri=%d",fri);

  printf("\nvalue of sat %d",x);

  getch();

}

 

Output:

sun=0

fri=12

value of sat 13

 

Example explained:

Here constants are initialized from "0"

Sequence is explicitly changed by assigning a value to "wed"

"x" is an enum type of variable can be only assigned with enum type of value.

 

Example2:

 

#include<stdio.h>

enum type{dom=1,com,ind,agr};

void main()

{

  enum type ch;

  int cmr,pmr,nu;

  float bill;

  clrscr();

  printf("Current month reading:");

  scanf("%d",&cmr);

  printf("Previous month reading:");

  scanf("%d",&pmr);

  nu=cmr-pmr;

  printf("Type of connection:\n");

  printf("1.Domestic\n2.Commercial\n3.Industrial\n

                                              4.Agriculture\nEnter yor choice 1..4:");

  scanf("%d",&ch);

  switch(ch)

  {

     case dom:

       bill=nu*3.75;

       break;

     case com:

       bill=nu*6.25;

       break;

     case ind:

       bill=nu*12.50;

       break;

     case agr:

       bill=nu*0;

  }

  printf("\nNo.of units %d",nu);

  printf("\nTotal bill %f",bill);

  getch();

}

 

Execution:

Current month reading:500

Previous month reading:200

Type of connection:

1.Domestic

2.Commercial

3.Industrial

4.Agriculture

Enter yor choice 1..4:2

 

No.of units 300

Total bill 1875.000000

 

Example explained:

Here "ch" is an enum type of variable compared with enum type of constants in switch case.

 

 

 

 

 

Typedef:

It is keyword used to rename an existed or user define type.

It is used to improve the readability of application.

 

Syntax:

typedef  <existed type> <new type>

 

Example:

 

#include<stdio.h>

number getsum(number,number);

void main()

{

   number a,b,c;

   clrscr();

   printf("Enter two numbers:\n");

   scanf("%d%d",&a,&b);

   c=getsum(a,b);

   printf("Sum %d",c);

   getch();

}

number getsum(number x,number y)

{

   return x+y;

}

                                                                                               

Example explained:

In the above example "int" type is renamed as "number"

"number" type is used along the program.

 

Example:

#include<stdio.h>

struct address

{

    char name[50];

    char street[50];

    char city[50];

    long int pin;

};

typedef struct address ads;

 

void main()

{

     ads x={"Black","M.G.Road","Hyd-bad",524201};

     clrscr();

     printf("Address:\n");

    printf("Name:%s\nStreet:%s\nCity:%s\nPin code:%ld",x.name,x.street,x.city,x.pin);

    getch();

}

 

Output:

Address:

Name:Black

Street:M.G.Road

City:Hyd-bad

Pin code:524201

 

Example explained:

Here "struct address" type is renamed as "ads"

0 comments:

Post a Comment